Storage Classes

From Vaddina.com

Jump to: navigation, search

Contents

std::strings

Initializing a string

string str = “C++;          // CORRECT
string str1(“C++);          // CORRECT
string str2 (this is cpp”); // CORRECT
string str3 (ptr);           // CORRECT
string str4 = ‘C’;           // WRONG
string str5 (1, ‘C’);        // CORRECT
string str6 (str);           // CORRECT

Resizing a string

Using the member function ‘resize’ to resize a string will add null characters, ‘\0’ to attain the required size.

string str = “cpp”;
str.resize (10);        // str = “cpp\0\0\0\0\0\0\0”;

Concatenating a string

string str = “C++;
string str1(“ is incredible”);
char *str2 = “ is incredible”;
char *str3 = “ and challenging.”;

string str += str1;          // str = C++ is incredible
string str += str2;          // str = C++ is incredible
string str = str + str2 + str3; // Error. Cannot concatenate strings str2 and str3, they being character strings.

string str = str + str2;
str += str3;                 // str = C++ is incredible and challenging.
string str.append(str1);     // str = C++ is incredible
Personal tools
Namespaces
Variants
Actions
Navigation
My Work
C/C++
Tutorials/Books/eBooks
Miscelleneous
Contact
Other
Toolbox