C++ basic string

C++ basic string

String construction

string();//Construct an empty string string.
string(const string &str);//Construct a string the same as str. Such as string s1(s2).
string(const char *s);//Initialize with string s
string(int n,char c);//initialize with n characters c

Character string description

int capacity()const;//Returns the current capacity (that is, the number of elements in the string that can be stored without increasing the memory)
int max_size()const;//Returns the length of the largest string that can be stored in the string object
int size()const;//Returns the size of the current string
int length()const;//Returns the length of the current string
bool empty()const;//Whether the current string is empty
void resize(int len,char c);//Set the current size of the string to len, and fill the insufficient part with character c
Input and output operations of the string class:
The overloaded operator operator>> of the string class is used for input, and the overloaded operator operator << is used for output operations.
The function getline(istream &in,string &s); is used to read a string from the input stream in to s, separated by a newline character'\n'.

Character operation

const char &operator[](int n)const;
const char &at(int n)const;
char &operator[](int n);
char &at(int n);
Both operator[] and at() return the position of the nth character in the current string, but the at function provides range checking, and out_of_range exception will be thrown when out of range. The subscript operator [] does not provide check access.
const char *data()const;//returns a non-null terminated c character array
const char *c_str()const;//returns a null-terminated c string
int copy(char *s, int n, int pos = 0) const;//Copy the n characters in the current string starting with pos to the character array starting with s, and return the actual number of copies

String assignment

string &operator=(const string &s);//Assign string s 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 string s to the current string
string &assign(int n,char c);//Use n characters c to assign the current string
string &assign(const string &s,int start, int n);//Assign n characters from start in string s to the current string

String concatenation

string &operator+=(const string &s);//Connect the string s to the end of the current string
string &operator+=(const char *s);//Connect the string s to the end of the current string
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+=()
string &append(const string &s,int pos, int n);//Connect the n characters starting from pos in the string s to the end of the current string
string &append(int n, char c);//Add n characters to the end of the current string c

String comparison

nt compare(const string &s) const;//Compare with string s
int compare(const char *s) const;//Compare with string s
The compare function returns 1 when >, -1 when <, and 0 when ==. The comparison is case-sensitive. Refer to the dictionary order when comparing. The uppercase A is smaller than the lowercase a.

String lookup

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 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 starting from pos from back to front 
int rfind(const char *s, int pos=npos) const;
int rfind(const string &s, int pos=npos) const;
//rfind means reverse search, if it cannot be found, it returns -1

String replacement

string &replace(int pos, int n, const char *s);//Delete n characters starting from pos, and then insert the string s at pos
string &replace(int pos, int n, const string &s);//Delete n characters starting from pos, and then insert the string s at pos
void swap(string &s2);//Swap the value of the current string and s2

String deletion

iterator erase(iterator first, iterator last);//Delete all characters between [first, last) and return the position of the iterator after deletion
iterator erase(iterator it);//Delete the character pointed to by it and return the position of the iterator after deletion
string &erase(int pos = 0, int n = npos);//Delete n characters from the beginning of pos and return the modified string

String iterator

The string class provides an iterator for forward and backward traversal. The iterator provides a syntax for accessing individual characters, similar to pointer operations, and the iterator does not check the range. Use string::iterator or string::const_iterator to declare iterator variables. const_iterator is not allowed to change the content of the iteration. Common iterator functions are:

const_iterator begin()const;
iterator begin();//Returns the starting position of the string
const_iterator end()const;
iterator end();//Returns the position after the last character of the string
const_iterator rbegin()const;
iterator rbegin();//Returns the position of the last character of the string
const_iterator rend()const;
iterator rend();//returns before the first character position of string
Reference: https://cloud.tencent.com/developer/article/1437085 C++ Basic String-Cloud + Community-Tencent Cloud