3 STL - Common Containers
3.1 string container
3.1.1 Basic concepts of string
Nature:
- string is a C++ style string, and string is essentially a class
Difference between string and char *:
- char * is a pointer
- String is a class, which encapsulates char*, manages this string, and is a container of char* type.
Features:
The string class encapsulates many member methods
For example: find find, copy copy, delete delete replace replace, insert insert
string manages the memory allocated by char*, don't worry about copying out-of-bounds and value out-of-bounds, etc., the class is responsible for it
3.1.2 string constructor
Constructor prototype:
- string(); //Create an empty string Example: string str;
string(const char* s); //Initialize with string s - string(const string& str); //Use one string object to initialize another string object
- string(int n, char c); //initialize with n characters c
Example:
#include<iostream> using namespace std; #include<string> //string constructor //string(); //Create an empty string Example: string str; //string(const char* s); //Initialize with string s //string(const string& str); //Use one string object to initialize another string object //string(int n, char c); //initialize with n characters c void test1() { string s1;//Default construction creates empty string, calls no-argument constructor const char* str = "hello world"; string s2(str);//Convert c_string to string cout << s2 << endl; string s3(s2);//call copy constructor cout << s3 << endl; string s4(10, 'a'); cout << s4 << endl; } int main() { test1(); system("pause"); return 0; }
Summary: The various construction methods of string are not comparable, and they can be used flexibly
3.1.3 string assignment operation
Function description:
- assign value to string
Function prototype for assignment:
- string& operator=(const char* s); //Char* type string is assigned to the current string
- string& operator=(const string &s); //Assign the string s to the current string
- string& operator=(char c); //The character is assigned to the current string
- string& assign(const char *s); //Assign the string s to the current string
- string& assign(const char *s, int n); //Assign the first n characters of string s to the current string
- string& assign(const string &s); //Assign the string s to the current string
- string& assign(int n, char c); //Assign n characters c to the current string
Example:
#include<iostream> using namespace std; #include<string> //string assignment operation /* string& operator=(const char* s); //char*type string is assigned to the current string string& operator=(const string &s); //assign the string s to the current string string& operator=(char c); //character assigned to the current string string& assign(const char *s); //assign the string s to the current string string& assign(const char *s, int n); //Assign the first n characters of string s to the current string string& assign(const string &s); //assign the string s to the current string string& assign(int n, char c); //Assign n characters c to the current string */ void test1() { string str1; str1 = "hello world"; cout << "str1= " << str1 << endl; string str2; str2 = str1; cout << "str2= " << str2 << endl; string str3; str3 = 'a'; cout << "str3= " << str3 << endl; string str4; str4.assign("hello hhh"); cout << "str4= " << str4 << endl; string str5; str5.assign("hello hhhh", 3); cout << "str5= " << str5 << endl; string str6; str6.assign(str5); cout << "str6= " << str6 << endl; string str7; str7.assign(10, 'w'); cout << "str7= " << str7 << endl; } int main() { test1(); system("pause"); return 0; }
Summarize:
There are many assignment methods for string, operator= This method is more practical
3.1.4 string concatenation
Function description:
- Implements concatenating strings at the end of strings
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); //Connect the string s to the end of the current string
- string& append(const char *s, int n); //Connect 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);//The n characters starting from pos in the string s are connected to the end of the string
Example:
#include<iostream> using namespace std; #include<string> //string string concatenation /* string& operator+=(const char* str); //Overloading the += operator string& operator+=(const char c); //Overloading the += operator string& operator+=(const string& str); //Overloading the += operator string& append(const char *s); //concatenates the string s to the end of the current string string& append(const char *s, int n); //concatenates 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); //Concatenate n characters starting at pos in string s to end of string */ void test1() { string str1="I"; str1 += "love watching movies"; cout << "str1= " << str1 << endl; str1 += ':'; cout << "str1= " << str1 << endl; string str2="Fast & Furious"; str1 += str2; cout << "str1= " << str1 << endl; string str3 = "I"; str3.append("love"); cout << "str3= " << str3 << endl; str3.append("game hhh",4); cout << "str3= " << str3 << endl; //str3.append(str2); //cout << "str3= " << str3 << endl; //str3.append(str2, 0, 4); str3.append(str2, 6, 10); cout << "str3= " << str3 << endl; } int main() { test1(); system("pause"); return 0; }
Summary: There are many overloaded versions of string concatenation, just remember a few in the beginner stage
3.1.5 string find and replace
Function description:
- Find: Find whether the specified string exists
- replace: replace the string at the specified position
Function prototype:
- int find(const string& str, int pos = 0) const; //Find the first occurrence of str, starting from pos
- int find(const char* s, int pos = 0) const; //find the first occurrence of s, starting from pos
- int find(const char* s, int pos, int n) const; // Find the first position of the first n characters of s from the pos position
- int find(const char c, int pos = 0) const; //find the first occurrence of character c
- int rfind(const string& str, int pos = npos) const; //find the last position of str, starting from pos
- int rfind(const char* s, int pos = npos) const; //find the last occurrence of s, starting from pos
- int rfind(const char* s, int pos, int n) const; //find the last position of the first n characters of s from pos
- int rfind(const char c, int pos = 0) const; //find the last occurrence of character c
- string& replace(int pos, int n, const string& str); //replace n characters starting from pos to string str
- string& replace(int pos, int n,const char* s); //replace n characters starting from pos to string s
Example:
#include<iostream> using namespace std; #include<string> //find and replace /* int find(const string& str, int pos = 0) const;` //Find the first occurrence of str, starting from pos int find(const char* s, int pos = 0) const; ` //Find the first occurrence of s, starting from pos int find(const char* s, int pos, int n) const; ` //Find the first n characters of s from position pos for the first time int find(const char c, int pos = 0) const; ` //Find the first occurrence of the character c int rfind(const string& str, int pos = npos) const;` //Find the last position of str, starting from pos int rfind(const char* s, int pos = npos) const;` //Find the last occurrence of s, starting from pos int rfind(const char* s, int pos, int n) const;` //Find the last position of the first n characters of s from pos int rfind(const char c, int pos = 0) const; ` //Find the last occurrence of the character c string& replace(int pos, int n, const string& str); ` //Replace n characters starting from pos with the string str string& replace(int pos, int n,const char* s); ` //Replace n characters starting from pos with string s */ //find void test1() { string str = "abcdefg"; int pos = str.find("rr"); cout << "pos= " << str.find("rr") << endl;//4294967295 because unsigned -1's complement if (pos == -1) { cout << "Not found" << endl; } else { cout << "pos= " << pos << endl; } //rfind //Difference between rfind and find //rfind searches from right to left, find searches from left to right pos = str.rfind("e"); cout << "pos= " << pos << endl; } //2. Replace void test2() { string str1 = "abcdefg"; str1.replace(1, 3, "1111");//Characters from position 1 to position 3 are replaced with "1111" cout << str1 << endl; } int main() { test1(); test2(); system("pause"); return 0; }
Summarize:
- find search is from left to back, rfind from right to left
- find returns the first character position of the search after finding the string, and returns -1 if not found
- When replace is replacing, it is necessary to specify from which position, how many characters, and what kind of string to replace
3.1.6 string string comparison
Function description:
- comparison between strings
How to compare:
- String comparison is to compare by the ASCII code of the characters
= returns 0
> return 1
< return -1
Function prototype:
- int compare(const string &s) const; //Compare with string s
- int compare(const char *s) const; //Compare with string s
Example:
#include<iostream> using namespace std; #include<string> //string comparison void test1() { string str1 = "aello"; string str2 = "hello"; if (str1.compare(str2) == 0) { cout << "str1==str2" << endl; } else if (str1.compare(str2)==1) { cout << "str1>str2" << endl; } else { cout << "str1<str2" << endl; } } int main() { test1(); system("pause"); return 0; }
Summary: String comparison is mainly used to compare whether two strings are equal. It does not make much sense to judge who is bigger and who is smaller.
3.1.7 string character access
There are two ways to access a single character in a string
- char& operator[](int n); //Get characters by [] method
- char& at(int n); //Get the character through the at method
Example:
#include<iostream> using namespace std; #include<string> //string character access void test1() { string str = "hello"; cout << "str= " << str << endl; //1. Access a single character via [] for (int i = 0; i < str.size(); i++) { cout << str[i] << " "; } cout << endl; //2. Access a single character through the at method for (int j = 0; j < str.size(); j++) { cout << str.at(j) << " "; } cout << endl; //Modify a single character str[0] = 'x'; cout << str << endl;//xello str.at(1) = 'x'; cout << str << endl;//xxllo } int main() { test1(); system("pause"); return 0; }
Summary: There are two ways to access a single character in the string string, using [ ] or at
3.1.8 string insertion and deletion
Function description:
- Insert and delete characters in string
Function prototype:
- string& insert(int pos, const char* s); //insert string
- string& insert(int pos, const string& str); //insert string
- string& insert(int pos, int n, char c); //Insert n characters c at the specified position
- string& erase(int pos, int n = npos); // delete n characters starting from Pos
Example:
#include<iostream> using namespace std; #include<string> //String insertion and deletion void test1() { string str = "hello"; //insert str.insert(1, "111"); cout << str << endl;//h111ello //delete str.erase(1, 3); cout << str << endl; } int main() { test1(); system("pause"); return 0; }
**Summary:**Insert and delete start subscripts from 0
3.1.9 string substring
Function description:
- Get the desired substring from a string
Function prototype:
- string substr(int pos = 0, int n = npos) const; //return a string consisting of n characters starting from pos
Example:
#include<iostream> using namespace std; #include<string> //string string void test1() { string str = "abcdefg"; cout << str.substr(1, 3) << endl; } void test2() { string email = "hello@sina.com"; //Get user information from email address int pos = email.find("@"); string name = email.substr(0, pos); cout << "name: " << name << endl; } int main() { test1(); test2(); system("pause"); return 0; }
**Summary:** The flexible use of the substring function can obtain effective information in actual development