1String concept
²string is a string type of STL that is commonly used to represent strings. Before using string, the string is usually represented by char*. Both string and char* can be used to represent strings, so what is the difference between them?
Comparison of String and char*
²string is a class, char* is a pointer to a character.
String encapsulates the char*, which manages the string and is a char* type container.
²string does not consider memory release and out-of-bounds.
String manages the memory allocated by the char*. Each time a string is copied, the value is maintained by the string class, without worrying about copying out-of-bounds and value out of bounds.
²string provides a series of string manipulation functions (this will be discussed in more detail)
Find found, copy copy, delete erase, replace replace, insert
Constructors for 2string
² Default constructor:
String (); Constructs an empty string S1.
² Copy Constructor:
String (const string &str); Constructs a string that is the same as Str. such as STRINGS1 (S2).
² Constructors with parameters
String (const char *s); Class with the string S.
string (int N,char c); Initialize with n characters c
3string Access character operation
Character manipulation for the ²string class:
const char &operator[] (int n) const;
const char &at(int n) const;
char &operator[] (int n);
char &at(int n);
²operator[] and at () all return the nth character in the current string, but the two are different.
The main difference is that the at () throws an exception when it crosses the boundary, [] it returns (char) 0 when it crosses the border, and the compiler makes an error when it goes out of bounds. If your program wants to catch an exception through Try,catch, it is recommended to use at ().
4 getting the const char* from string
²const Char *c_str () const; Returns the first address of a string ending with '% '
5 Copying a string to an operation that char* points to a memory space
²int copy (char *s, int n, int pos=0) const;
Copies the n characters starting at Pos in the current string to a character array starting with S, returning the number of actual copies. Note that the space pointed to by S is large enough to accommodate the current string, otherwise it will go out of bounds.
Length of 6string
int length () const; Returns the length of the current string. Length does not include ' + ' at the end of the string.
BOOL empty () const; Whether the current string is empty
Assignment of 7string
string & operator = (const string & s); // Assign string s to the current string
string & assign (const char * s); // Assign string s to the current string
string & assign (const char * s, int n); // The first n characters of the string s are assigned to the current string
string & assign (const string & s); // Assign string s to the current string
string & assign (int n, char c); // Assign the current string with n characters c
string & assign (const string & s, intstart, int n); // Assign n characters from string s starting from start to the current string
8string String Connection
string & operator + = (const string & s); // Concatenate string s to the end of the current string
string & operator + = (const char * s); // Concatenate string s to the end of the current string
string & append (const char * s); // Concatenate string s to the end of the current string
string & append (const char * s, intn); // Concatenate the first n characters of string s to the end of the current string
string & append (const string & s); // Same as operator + = ()
string & append (const string & s, intpos, int n); // concatenate n characters from string s starting from pos to the end of the current string
string & append (int n, char c); // Add n characters at the end of the current string c
Comparison of 9string
int compare (const string & s) const; // Compare with string s
int compare (const char * s) const; // Compare with string s
The compare function returns 1 for>, -1 for <, and 0 for ==. The comparison is case-sensitive, and the order of the dictionary is compared when comparing. An uppercase A is smaller than a lowercase a.
Sub-string of 10string
string substr (int pos=0, int n=npos) const; Returns a substring of n characters starting at the POS
11string Find and replace
Find
int find (char c, int pos = 0) const; // Find the position of character c in the current string starting from pos
int find (const char * s, int pos = 0) const; // Find the position of the string s in the current string starting from pos
int find (const string & s, int pos = 0) const; // Find the position of the string s in the current string starting from pos
If the find function cannot find it, it returns -1
int rfind (char c, int pos = npos) const; // Find the position of the character c in the current string
int rfind (const char * s, int pos = npos) const;
int rfind (const string & s, intpos = npos) const;
// rfind means reverse lookup, if not found, return -1
Replace
string & replace (int pos, int n, constchar * s); // Delete n characters starting from pos, and then insert string s at pos
string & replace (int pos, int n, conststring & s); // Delete n characters starting from pos, and then insert string s at pos
void swap (string & s2); // Swap the current string and the value of s2
// 4 Find and replace strings
void main25 ()
{
strings1 = "wbm hello wbm 111 wbm 222 wbm 333";
size_tindex = s1.find ("wbm", 0);
cout << "index:" << index;
// Find the number of times itcast appears
size_toffindex = s1.find ("wbm", 0);
while (offindex! = string :: npos)
{
cout << "Under index:" offindex << "Wbm \ n" found;
offindex = offindex + 1;
offindex = s1.find ("wbm", offindex);
}
// replace
strings2 = "wbm hello wbm 111 wbm 222 wbm 333";
s2.replace (0,3, "wbm");
cout << s2 << endl;
// Find the number of times itcast appears
offindex = s2.find ("wbm", 0);
while (offindex! = string :: npos)
{
cout << "Under index:" offindex << "Wbm \ n" found;
s2.replace (offindex, 3, "WBM");
offindex = offindex + 1;
offindex = s1.find ("wbm", offindex);
}
cout << "S2 after replacement:" << s2 << endl;
}
12String interval Delete and insert
string & insert (int pos, const char * s);
string & insert (int pos, const string & s);
// The first two functions insert the string s at the position pos
string & insert (int pos, int n, charc); // insert n characters at position pos
string & erase (int pos = 0, intn = npos); // Remove the n characters starting with pos and return the modified string
13string algorithm Correlation
void main27()
{
strings2 = "AAAbbb";
transform(s2.begin(),s2.end(), s2.begin(), toupper);
cout<< s2 << endl;
strings3 = "AAAbbb";
transform(s3.begin(),s3.end(), s3.begin(), tolower);
cout<< s3 << endl;
}
The above is the C + + Review Essentials summary Z of the 12--stl string content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!