I. essence of string
In fact, string is not a separate container, it is just a typedef of the basic_string template class, and the corresponding wstring, you will find the following code in the string header file:
Extern "C ++ "{
Typedef basic_string String;
Typedef basic_string Wstring;
} // Extern "C ++"
2. Construct a string
Char allarray [] = "all's well that ends well ";
String five (allarray, 20 );
String six (allarray + 6, allarray + 10 );
Prototype: string (const char * s, size_type N)
Forcibly copy n strings from S. If S is less than n characters long, useless characters are still copied.
Prototype: Template String (ITER begin, ITER end)
Since the array name is equivalent to a pointer, all types of allarray + 6 and allarray + 10 are char *. Therefore, when using the template, replace the type of char * with ITER. Six is initialized to well.
String seven (five + 6, five + 10); // This statement is incorrect, and the object name is not looked at by the object address. Five is not a pointer, five [6] is a char value, so & five [6] is an address.
String seven (& five [6], & five [10]);
Iii. string type input
For C-style:
Char info [6];
Cin. Getline (Info, 6); // read a line, discard/n
Cin. Getline (Info, 6, ":"); // read up to:, discard:
For string:
String sbuff;
Cin. Getline (CIN, Info); // read a line, discard/n
Cin. Getline (CIN, info, ":"); // read up to:, discard:
The main difference between them: the string version of Getline will automatically adjust the size of the target String object. The maximum allowed length of a string object, which is specified by string: NPOs. This is usually the maximum unsigned int value.
Iv. Use of the sting class
String provides rich search functions such as find (find_first_of, find_last_o and substr), insert, replace, and erase.
The c_str () function of string is used to obtain a C-language string, and its returned pointer cannot modify its space. In addition, a new pointer is obtained after the next call.
The string pointer returned by the data () function of string will not end with '/0', and must be ignored.
To filter all non-English characters at the beginning and end of a line. Let's see how to use string:
# Include
# Include
Using namespace STD;
Int main (){
String strinfo = "// * --- Hello word !...... ------";
String strset = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz ";
Int first = strinfo. find_first_of (strset );
If (first = string: NPOs ){
Cout return-1;
}
Int last = strinfo. find_last_of (strset );
If (last = string: NPOs ){
Cout return-1;
}
Cout return 0;
}
V. Conversion between cstring, Int, String, char *
1. String-> cstring
Cstring. Format ("% s", String. c_str ());
2. Char-> cstring
Cstring. Format ("% s", char *);
3. Char-> string
String S (char *);
4. String-> char *
Char * P = string. c_str ();
5. cstring-> string
String S (cstring. getbuffer ());
6. String-> cstring
Cstring. Format ("% s", String. c_str ());
C_str () is indeed better than data.
7. Char-> string
String S (char *);
You can only initialize it. It is best to use assign () unless it is initialized ().
8. cstring-> string
String S (cstring. getbuffer ());
Releasebuffer () is required after getbuffer (). Otherwise, no space occupied by the buffer is released.
9. Convert the character to an integer. You can use atoi, _ atoi64, or atol.
10. cstring conversion char [100]
Char A [100];
Cstring STR ("aaaaaa ");
Strncpy (A, (lpctstr) STR, sizeof ());
Appendix 6
String function list Function Name Description
Get the iterator pointing to the beginning of the string
End obtains the iterator pointing to the end of the string.
Rbegin obtains the iterator pointing to the beginning of the reverse string.
Rend to get the iterator pointing to the end of the reverse string
Returns the size of the string.
The length and size functions are the same.
Max_size the maximum possible size of a string
Possible size of the string without allocating memory again
Empty determines whether it is null
OPERATOR [] obtains the nth element, which is equivalent to an array.
C_str: returns the C-style const char * string.
Data obtains the string content address
Operator = value assignment operator
Reserve reserved space
Swap Functions
Insert characters
Append characters
Push_back append characters
Operator ++ = Operator
Erase
Clear all content in the character container
Resize re-allocates Space
Assign is the same as the value assignment operator.
Replace substitution
Copy string to Space
Find
Rfind Reverse Lookup
Find_first_of finds any character in the substring and returns the first position.
Find_first_not_of searches for any character that does not contain a substring and returns the first position.
Find_last_of finds any character in the substring and returns the last position.
Find_last_not_of searches for any character that does not contain a substring and returns the last position.
Obtain the string from substr.
Compare comparison string
Operator + String Link
Operator = judge whether it is equal
Operator! = Judge whether it is not equal
Operatoroperator> Read strings from the input stream
Operatorgetline reads a row from the input stream