string container
Basic string concept
nature:
string is a C++-style string. Unlike the C language char*, it is essentially a class.
The difference between string and char*:
char* is a pointer
string is a class that encapsulates char* to manage strings inside, and is a char& type container
Features:
Strit class encapsulates many member methods internally
For example: find, copy copy, delete delete, replace replace, insert insert
string manages the memory allocated by char*, and does not need to consider issues such as assignment and value cross-border. It is responsible for the class internally.
string constructor
Four function prototypes
- string() creates an empty string
- string(const char*s) is initialized using string s
- string(const string& str) uses a string object to initialize another string object
- string(int n, char c) is initialized with n characters c
Example of usage:
//String's constructorvoid test1() { string s1;//Default construct const char* str = "Leaves fall, autumn white"; string s2(str); cout << "s2:"<<s2 << endl; string s3(s2); cout << "s3" << s3 << endl; string s4(6,'a'); }
The above is an example corresponding to the four construction methods. The first method is used frequently. The second method is to set an immutable character array to initialize it. The third method is to call a copy structure. The fourth method is more interesting. The meaning in the above code is to use 6 'a' to initialize the string. The result of inputting s4 is: aaaaaa.
string assignment operation
Assign value to string string
Function prototype for assignment:
- string& operator = (const char* s)char* type string Assign to the current string
- string& operator = (const string &s) Assign string s to the current string
- string& operator = (char c) Assign character to the current string
- string& assign(const char* s) assigns string s to the current string
- string& assign(const char*s,int n) assign the current n characters of string s to the current string
- string& assign(const string &s) assign string s to the current string
- string& assign(int n, char c) assigns n characters c to the current string
Example of usage:
void test2() { string str1; str1 = "The leaves fall in autumn white"; cout << "str1=" << str1 << endl; string str2; str2 = str1; cout << "str2=" << str2 << endl; string str3; str3 = 'c'; cout << "str3=" << str3 << endl; string str4; ("hello c++"); cout << "str4=" << str4 << endl; string str5; ("hello c#",5); cout << "str5=" << str5 << endl; string str6; (str5); cout << "str6=" << str6 << endl; string str7; (6, 'w'); cout << "str7=" << str7 << endl; }
Tips:stirng assignment methods are many, but the overloaded operator= method is most commonly used.
string splicing operation
Stitching string at the end of the string
Function prototype:
- string& operator+=(const char* str)overload+=operator
- string& operator+=(const char c) overload+= operator
- string& operator+=(const string& str)overload+= operator
- string& append(const char* s) concatenate string s to the end of the current string
- string& append(const char* s, int n) concatenate the first n characters of string s to the end of the current string
- string& append(const string &s) same as operator+=(const string& str)
- string& append(const string &s,int pos,int n) Connect n characters in string s starting from pos to the end of the string
Example of usage:
void test3() { string str1 = "red beans"; str1 += "Remembering Love"; cout << "str1=" << str1<< endl; str1 += '?'; cout << "str1=" << str1 << endl; string str2 = "yyds"; str1 += str2; cout << "str1=" << str1 << endl; string str3 = "You"; ("low"); cout << "str3=" << str3 << endl; ("wuwuwu qaq", 4); cout << "str3=" << str3 << endl; (str2); cout << "str3=" << str3 << endl; (str2, 0, 1); cout << "str3=" << str3 << endl; }
Tips: Beginners only need to remember a few splicing functions
string search replacement
Specify location to find string
Delete string at specified location
Function prototype:
1. Find the first occurrence of s, and start looking at pos
int find(const string& str, int pos = 0) const;
int find(const char* s , int pos ==0) const;
2. Find the first n characters of s from the pos position
int find( const char* s, int pos, int n) const;
3. Find the first occurrence location of character c
int find(const char c, int pos = e) const;
4. Find the last location of str, start from pos
int rfind(const string& str, int pos = npos) const;
5. Find the last position of str, start from pos, and count always starts from front to back
int rfind(const char* s, int pos = npos) const;
6. Find the last position of the first n characters of s from pos
int rfind(const char* s, int pos, int n) const;
7. Find the last occurrence of character c
int rfind(const char c, int pos - e) const;
8. Replace n characters starting from pos as string str
string& replace(int pos, int n, const string& str);
9. Replace n characters starting from pos as string s
string& replace(int pos, int n,const char* s );
Example of usage:
//Search and replacement of strings//Findvoid test4() { string str1 = "abcdefgh"; //Return subscript is found, return -1 cannot be found int pos1 = ("def"); cout << "pos1=" << pos1 << endl; int pos2 = ("s"); cout << "pos2=" << pos2<< endl; pos1 = ("ab");//Finish the first appearance from right to left and count from left to right cout << "pos1=" << pos1 << endl;; } //replacevoid test5() { string str2 = "abcdef"; (1, 2, "1111");//From the position 1, 2 characters are replaced by 1111 cout << "str2=" << str2 << endl; }
tips:
Find find the string and returns the first character position found, and return 1 if it cannot be found.
Although there are many functions, almost all of them are in two versions, one is in the C++ style and the other is in the C language style.
string string comparison
String comparison is based on ASCII code of characters
Function prototype:
int compare(const string &s) const; int compare(const char* s) const;
Example of usage:
string str1 = “zello”; string str2 = “hello”; if ((str2) == 0) { cout << “equal” << endl; } else if ((str2) > 0) { cout << “str1big” << endl; } else { cout << “str2big” << endl; }
tips: The purpose of string comparison is to compare whether two strings are equal, and the meaning of judging who is bigger and who is smaller is not very important.
string character reading
There are two ways to access a single character:
Function prototype:
char& operator[] (int n); //Get characters through []char& at (int n); //Get characters through the at method
Example of usage:
string str1 = “hello”; //Accessing a single character through []for (int i = 0; i < (); i++) { cout << str1[i] << " "; } cout << endl; //Single character accessed through atfor (int i = 0; i < (); i++) { cout << (i) << " "; } cout << endl; //Modify single characterstr1[0] = ‘z'; cout << str1 << endl; (0) = ‘x'; cout << str1 << endl;
string insertion and deletion
Function prototype:
string& insert(int pos,const cahr* s);//Insert string at n positionstring& insert(int pos,const string& s);//Insert string at n positionstring& insert(int pos,int n,char c);//Insert n characters c at the specified positionstring& erase(int pos,int n = npos);//Delete n characters starting from the pos position
Example of usage:
string str = “hello”; //insert(1, “111”); cout << "str = " << str << endl; //delete(1,3); cout << "str = " << str << endl;
tips: The starting subscripts for insertion and deletion start from 0.
string seeking child string
Get the desired substring from the string
Function prototype:
string substr(int pos=0,int n=npos) const ;//Returns a string composed of n characters starting from the pos position
//StringSearch for child stringvoid test01() { string str = "abcdef"; string subStr = (1, 3); cout << "subStr=" << subStr << endl; } //Usage operationvoid test02() { string email = "ylqb@"; //Get username information from email address int pos = ("@"); string usrName = (0, pos); cout << usrName << endl; }
tips: Flexible use of the substring function, you can obtain effective information in actual development, and you can effectively obtain user names in different mailboxes in the above code.
This is the article about understanding the structure and use of string containers in C++. For more related C++ string container content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!