c ++ string class

Initialization: The initialization of the string class cannot be performed with characters, such as; string str = 'c'; string str ('c');
string literals must be passed as parameters;
string itself is instantiated with the template class class.

The string class is variable-length using the length () method to return the length of the object. At the same time, the string class is different from the char array in the c language. There is no \ 0 at the end. If you want to transform, use the c_str method, which returns a char array type. Strings. You can also use subscripts to access [] similar to character arrays. Or use the method at (); for
example str.at (i) = 'c'; str [i] = 'c'; // But the use of brackets does not Will determine whether the out-of-bounds access, but at () method can determine whether out-of-bounds occurs, but at () is slower, to check.

assign member function:
string s1 ("cat"), s3;
s3.assign (s1); // copy s1 to s3;
s3.assign (s1,1,3); // subscript one from s1 The characters start to copy the string of length three in s1 to s3

append ():
You can connect the string normally or similar to the assign. You can copy the specified length and the beginning of the string and connect to the back
s1.append (s2);
s2.append (s1,3, s1.size ()); / S1.size () characters starting from the subscript three, if there are not enough numbers in the string, copy to the last character and connect to the end of s2.
compare ();
s1.compare (s2); // if s2.length () <s1.length (), return 1;
if s2.length () == s1.length (), return 0; if s2.length ()> s1.length (), return -1;
You can also compare a part between two strings.
substr ():
s1.substr (0,4); // Start from subscript 0, length 4 Characters to return a substring.
Swap ():
s1.swap (s2); // exchange the context between the two string.

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/zy1120192493/p/12739733.html