C++STL provides a string base character series container to handle strings, which can be interpreted as string classes, providing rich methods for adding delete \ replace \ Find and compare.
Using the string container, you need to include the header file declaration # include
Name of function |
Description |
Begin |
Get the iterator that point to the beginning of the string |
End |
Get the iterator that points to the end of the string |
Rbegin |
Get the iterator that point to the beginning of the reverse string |
Rend |
Gets the iterator that points to the end of the reverse string |
Size |
Get the size of the string |
Length |
Same function as size function |
Max_size |
The maximum possible size of a string |
Capacity |
The possible size of a string without reallocating memory |
Empty |
Determines whether the empty |
C_str |
Get the C-style const char* string |
Data |
Get string content Address |
Operator= |
Assignment operator |
Swap |
Swap functions |
Insert |
Insert character |
Append |
Append character |
Push_back |
Append character |
operator+= |
+ = operator |
Erase |
Delete a string |
Clear |
Empty all contents of the character container |
Resize |
Reassign Space |
Assign |
Same as the assignment operator |
Replace |
Alternative |
Copy |
String to Space |
Find |
Find |
RFind |
Reverse Lookup |
Find_first_of |
Finds any character in the containing substring and returns to the first position |
Find_first_not_of |
Find any characters that do not contain substrings, return to the first position |
Find_last_of |
Finds any character in the containing substring and returns to the last position |
Find_last_not_of |
Find any characters that do not contain substrings, and return to the last position |
Substr |
Get substring |
Compare |
Comparing strings |
operator+ |
String links |
operator== |
Judging if they are equal |
operator!= |
Judging if not equal to |
operator< |
Judging if it is less than |
Operator>> |
Reads a string from the input stream |
operator<< |
String writes to the output stream |
Getline |
Reads a line from the input stream |
1. Creating a String Object
string s; //生成一个空字符串sstring s(str) //拷贝构造函数 生成str的复制品string s(str,stridx) //将字符串str内"始于位置stridx"的部分当作字符串的初值string s(str,stridx,strlen) //将字符串str内"始于stridx且长度顶多strlen"的部分作为字符串的初值string s(cstr) //将C字符串作为s的初值string s(chars,chars_len) //将C字符串前chars_len个字符作为字符串s的初值。string s(num,c) //生成一个字符串,包含num个c字符string s(beg,end) //以区间beg;end(不包含end)内的字符作为字符串s的初值
A Length_error exception is thrown when the constructed string is too long to be expressed;
2. Accessing the elements of a string object
The elements of a string object are generally accessed randomly using subscript, and the subscript is counted from 0, and the string object element is a character (char), which must be clear;
const char &operator[](int n)const;const char &at(int n)const;char &operator[](int n);char &at(int n);//operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。const char *data()const;//返回一个非null终止的c字符数组const char *c_str()const;//返回一个以null终止的c字符串int copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目string s = "abc123456";cout << s[0] << endl; //输出对象的首元素cout << s[0] - ‘a‘ << endl; //两个相等的字符相减为0;
Method A of the 3.string Class A) =,assign ()//assigns a new value
s = "hello";s.assign("hello"); // basic_string& assign(const value_type* __s);s.assign(str); s.assign(str,1,3); //如果str是"iamangel" 就是把"ama"赋给字符串s.assign(str,2,string::npos); //把字符串str从索引值2开始到结尾赋给ss.assign("nico",5); //把‘n‘ ‘I‘ ‘c‘ ‘o‘ ‘\0‘赋给字符串s.assign(5,‘x‘); //把五个x赋给字符串string &operator=(const string &s);//把字符串s赋给当前字符串string &assign(const char *s);//用c类型字符串s赋值string &assign(const char *s,int n);//用c字符串s开始的n个字符赋值string &assign(const string &s);//把字符串s赋给当前字符串string &assign(int n,char c);//用n个字符c赋值给当前字符串string &assign(const string &s,int start,int n);//把字符串s中从start开始的n个字符赋给当前字符串string &assign(const_iterator first,const_itertor last);//把first和last迭代器之间的部分赋给字符串
b) Swap ()//Exchange two strings of content
void swap(string &s2); //交换当前字符串与s2的值
c) +=,append (), push_back ()//Add characters at the tail
s += str; //加个字符串s += "my name is jiayp"; //加个C字符串s += ‘a‘; //加个字符s.append(str);s.append(str,1,3); //截取str的第1~3位的字符串附在s的后面s.append(str,2,string::npos) //截取str的第二到末尾的字符串附在s的后面string &operator+=(const string &s);//把字符串s连接到当前字符串的结尾 string &append(const char *s); //把c类型字符串s连接到当前字符串结尾string &append(const char *s,int n);//把c类型字符串s的前n个字符连接到当前字符串结尾string &append(const string &s); //同operator+=()string &append(const string &s,int pos,int n);//把字符串s中从pos开始的n个字符连接到当前字符串的结尾string &append(int n,char c); //在当前字符串结尾添加n个字符cstring &append(const_iterator first,const_iterator last);//把迭代器first和last之间的部分连接到当前字符串的结尾s.push_back(‘!‘) //这个函数只能增加单个字符
d) Insert ()//Insert character
string &insert(int p0, const char *s);string &insert(int p0, const char *s, int n);string &insert(int p0, const string &s);string &insert(int p0, const string &s, int pos, int n);//前4个函数在p0位置插入字符串s中pos开始的前n个字符string &insert(int p0, int n, char c);//此函数在p0处插入n个字符citerator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置void insert(iterator it, const_iterator first, const_iterator last);//在it处插入[first,last)之间的字符void insert(iterator it, int n, char c);//在it处插入n个字符c
e) Erase ()//delete character
iterator erase(iterator first, iterator last); //删除[first,last)之间的所有字符,返回删除后迭代器的位置iterator erase(iterator it);//删除it指向的字符,返回删除后迭代器的位置string &erase(int pos = 0, int n = npos);//删除pos开始的n个字符,返回修改后的字符串string s = "abc123456";string::iterator it = s.begin(); //定义迭代器变量,指向字符串对象首元素;s.erase(it+3); //删除第三个元素,元素从位置0开始计数;s.erase(it, it+4); //删除区间[0, 4)内所有元素的值;s.erase(s.begin(), s.begin()+4); //删除区间[0, 4)内所有元素的值;s = ""; //清空字符串,或者直接用s.clear();
f) Clear ()//delete all characters g) replace ()//Replace character
String &replace (int p0, int n0,const char *s); Delete N0 characters starting from P0, and then insert a string sstring &replace (int p0, int n0,const char *s, int n) at P0; Delete the N0 characters that start with P0, and then insert the first n characters of the string s at P0 string &replace (int p0, int n0,const string &s); Delete N0 characters starting from P0, and then insert a string sstring &replace (int p0, int n0,const string &s, int pos, int n) at P0; Delete the n0 characters that begin with P0, and then insert string s in the P0 string, starting with the N characters from the Pos. &replace (int p0, int n0,int n, char c); Remove N0 characters starting with P0, then insert n characters at P0 CString &replace (iterator first0, iterator last0, const char *s); Replace the section between [First0,last0] with the string sstring &replace (iterator first0, iterator last0, const char *s, int n); Replace the section between [First0,last0] with the first n characters of S, String &replace (iterator first0, iterator last0, const string &s); Replace the section between [First0,last0] with the string sstring &replace (iterator first0, iterator last0, int n, char c); Replace the section between [First0,last0] with n characters CString &replace (iterator first0, iterator last0, const_iterator first, Const_ Iterator last); Replace the part between [First0,last0] with [First,last) between the strings
h) +//concatenation string i) ==,!=,<,<=,>,>=,compare ()//Compare String
bool operator==(const string &s1,const string &s2)const;//比较两个字符串是否相等//运算符">","<",">=","<=","!="均被重载用于字符串的比较;int compare(const string &s) const;//比较当前字符串和s的大小int compare(int pos, int n, const string &s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小int compare(int pos, int n, const string &s, int pos2, int n2)const;//比较当前字符串从pos开始的n个字符组成的字符串与s中pos2开始的n2个字符组成的字符串的大小int compare(const char *s) const;int compare(int pos, int n, const char *s) const;int compare(int pos, int n, const char *s, int pos2) const;
The Compare function returns -1,== when > returns 1,< when return is 0
j) Size (), Length ()///return number of characters K) max_size ()///The maximum number of characters to return L) empty ()//Determine if the string is empty
cout << s.empty() << endl;s.clear(); //或者是 s = "";cout << s.empty() << endl;
m) Capacity ()//return the character capacity before redistribution N) reserve ()//retain a certain amount of memory to accommodate a certain number of characters
string s = "0123456789";reverse(s.begin(), s.end());cout << s << endl;
o) [], at ()//Access single character P) >>,getline ()//read a value from stream Q) <<//write a value to STREAMR) copy ()//Assign a value to a c_strings) c_str ()// Returns the content as C_string T) data ()//returns the content as a character array U) substr ()//Returns a substring
string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串
V) Find function
int find (char c, int pos = 0) const;//from the Pos to find the character C at the position of the current string int find (const char *s, int pos = 0) const;//from the Pos to find the string s bit in the current string Set int find (const char *s, int pos, int n) const;//from Pos to find the position of the first n characters in the string s in the current string, int find (const string &s, int pos = 0) Cons t;//from Pos to find the position of the string s in the current string//Find successful when the location returned, failed to return the value of String::npos int rfind (char c, int pos = NPOs) const;// From the POS, look forward to the character C in the current string position int rfind (const char *s, int pos = NPOs) const;int rfind (const char *s, int pos, int n = NPOs) cons T;int rfind (const string &s,int pos = NPOs) const;//The position of the first n characters in the string s in the current string from the beginning of the POS, from the back forward, and returns a string when unsuccessful: The value of NPOs int find_first_of (char c, int pos = 0) const;//from the Pos to find the first occurrence of the character C position int find_first_of (const char *s, int pos = 0) con St;int find_first_of (const char *s, int pos, int n) const;int find_first_of (const string &s, int pos = 0) const;//from POS Begins the search for the position of the character in the first n-character array of s in the current string. Lookup failed to return String::npos int find_first_not_of (char c, int pos = 0) const;int find_first_not_of (const char *s, int pos = 0) Cons T;int Find_first_not_oF (const char *s, int pos, int n) const;int find_first_not_of (const string &s, int pos = 0) const;//Find the first character not in string s from the current string Present position, failed return String::npos int find_last_of (char c, int pos = NPOs) const;int find_last_of (const char *s, int pos = NPOs) const ; int find_last_of (const char *s, int pos, int n = npos) const;int find_last_of (const string &s, int pos = NPOs) const; int find_last_not_of (char c, int pos = NPOs) const;int find_last_not_of (const char *s, int pos = NPOs) const;int Find_las t_not_of (const char *s, int pos, int n) const;int find_last_not_of (const string &s, int pos = NPOs) const;//find_last_ The and find_last_not_of are similar to find_first_of and find_first_not_of, except that they look forward to cout << s.find (' C ') << Endl; Finds the first character ' C ', returns its subscript value; cout << s.find ("C") << Endl; Finds the first substring "C", returns its subscript value; cout << s.find ("Cat") << Endl; Finds the first substring "cat", returns its subscript value; cout << s.find ("DOGC") << Endl; Not found, returned;
W) begin () end ()//provide an STL-like iterator support
const_iterator begin()const;iterator begin(); //返回string的起始位置const_iterator end()const;iterator end(); //返回string的最后一个字符后面的位置
x) Rbegin () rend ()//Reverse iterator
const_iterator rbegin()const;iterator rbegin(); //返回string的最后一个字符的位置const_iterator rend()const;iterator rend(); //返回string第一个字符位置的前面//rbegin和rend用于从后向前的迭代访问,通过设置迭代器string::reverse_iterator,string::const_reverse_iterator实现
Y) Get_allocator ()//Return Configurator
string--c++ STL Learning