1, string introduction
#include<string>
using namespace std;
2, Definition of string
- Defining a string is the same as the basic data type. You only need to follow the variable name after the string
- Initialization can be achieved directly by assigning a value to a variable of type string
string str;
string str = "abc";
3, Access to content in string
- Through subscript access, you can access string s like character arrays
- Accessed through iterators, string, like vector, supports adding and subtracting a number directly from iterators, such as str.begin()+1
- If you want to input or output an entire string, you can only use cin and cout
#include<stdio.h>
#include<string>
using namespace std;
int main(){
string str = "abcd";
//Access by subscript
for(int i = 0; i < str.length(); i++){
printf("%c", str[i]);
}
//Access via iterator
for(string::iterstor it = str.begin(); it != str.end(); it++;){
printf("%c", *it);
}
return 0;
}
4, string common functions
- Operator + and operator + =: spell two string s directly
string str1 = "abc";
string str2 = "ab";
str1 + str2 ; //"abcab"
str1 += str2; //"abcab"
- compare operator: two string types can be used directly = =,! =, < =, > =, <, > Compare size. The comparison rule is dictionary order
string str1 = "aa", str2 = "aa", str3 = "abc", str4 ="dc"
if(str1 == str2) printf("Yes!");
if(str1 <str2) printf("Yes!");
if(str4 >= str3) printf("Yes!");
//All outputs Yes!
- Both length() and size() return the length of string, that is, the number of stored characters. The time complexity is O(1)
- insert(): inserts a character at a specified position. The time complexity is O(n)
- insert(pos, string), insert string at pos number
- Insert (it, it2, it3). It is the position where the original string is to be inserted. It2 and it3 are the beginning and end iterators of the string to be inserted, which is used to indicate that the string [it2, it3) will be inserted at the position of it.
//Example 1
string str = "abcd", str2 = "xyz";
str.insert(2, str2);//Insert str2 at str[2]
//abxyzcd
//Example 2
string str = "abcd", str2 = "xyz";
str.insert(str.begin() + 2, str2.begin(), str2.end());
//abxyzcd
- erase(): delete element, time complexity O(n)
- Delete single element: str.erase(it) is used to delete a single element. It is the iterator of the element to be deleted
- Delete all elements in an interval: str.erase(first, last), where first is the start iterator of the interval to be deleted, and last is the next address of the end iterator of the interval to be deleted, that is, delete [first, last)
- Delete the len characters starting from a certain position: str.erase(pos, length), where pos is the starting position to be deleted and length is the number of characters to be deleted.
string str = "abcdefgh";
str.erase(str.begin()+2); //Delete str[2],abdefgh
str.erase(str.begin()+2, str.end()-1);//Delete str[2, 6),abh
str.erase(1,2);//Delete the first two characters of str[1], a
- clear(): clear the data in the string. The time complexity is O(1)
- substr(pos, len): returns a substring of length len starting from pos, with a time complexity of O(len)
string str = "Hello world";
str.substr(0, 5); //Hello
- find(): find substring
- str.find(str2). When str2 is a substring of STR, it returns the position where it first appears in str; If str2 is not a substring of STR, string::npos is returned
- str.find(str2, pos) matches str2 from the pos number of str.
- The time complexity is O(nm), where n and m are the lengths of str and str2, respectively
- string::npos is a constant with its own value of - 1, but it is unsigned_int type, which can actually be considered unsigned_ The maximum value of int. string::npos is used as the return value when the find function fails. It can be considered as - 1 or 4294967295
#include<string>
#include<iostream>
using namespace std;
int main(){
string str = "Hello world";
string str2 = "Hello";
string str3 = "Hi";
if(str.find(str2) != string::npos)
cout<<str.find(str2)<<endl;//Output 0
if(str.find(str2, 3) != string::npos)
cout<<str.find(str2, 3)<<endl;
if(str.find(str3) != string::npos)
cout<<str.find(str3)<<endl;
else
cout<<"No"<endl;//Output No
return 0;
}
- replace()
- str.replace(pos, len, str2) replaces the substring of str with the length of len starting from the pos position with str2
- str.replace(it1, it2, str2) replaces the substring of the iterator [it1, it2) range of str with str2
- Time complexity O(str.length())
#include<iostream>
#include<string>
using namespace std;
int main(){
string str = "Life will be fine";
string str2 = "will not";
string str3 = "bad";
cout<<str.replace(5,4, str2)<<endl;
cout<<str.replace(str.begin()+, str.end(), str3)<<endl;
return 0;
}