17. C ++-string class (detailed description), 17. c-string
C ++ string class
In the C language, strings are represented by character arrays. For the application layer, strings are often used. Continuing to use character arrays reduces the efficiency.
Therefore, in the C ++ standard libraryClass stringNew custom string.
Header file:# Include <string>
- StringString connection
- StringString size comparison
- StringSubstring search and Extraction
- String directly supports stringInsert and replace
Common constructors of the string class include:
String str; // generate an empty string str ("ABC") // equivalent to str = "ABC"
String str ("ABC", strlen) // save "ABC" to str. A maximum of the first strlen bytes of string s ("ABC", stridx, strlen) can be stored) // store the stridx position of "ABC" as a string starting with str. A maximum of strlen bytes can be stored. string s (strlen, 'A') // store strlen 'A' to str
Common member functions of the string class include:
Str1.length (); // obtain str1.swap (str2); // replace str1.puch _ back ('A') of str1 and str2 '); // Add A 'A' character at the end of str1. The parameter must be in the character form str1.append ("ABC"); // Add A "ABC" string at the end of str1, the parameter must be in the string format str1.insert ("ABC", 2); // Insert "ABC" str1.erase (2) at the subscript of str1 2 ); // Delete the position where the subscript is 2, for example, "ABCD" --> "AB" str1.erase (); // delete one from the position where the subscript is 2, for example: "ABCD" --> "ABD" str1.clear (); // Delete All str1.replace (2, 4, "ABCD"); // Replace four bytes from the position where the subscript is 2, is "ABCD" str1.empty (); // if it is null, true is returned.
Reverse correlation (In header file <algorithm>)
String str ("hello"); reverse (str. begin (), str. end (); cout <str <endl; // reverse