current position:Home>C + + string container

C + + string container

2022-08-06 19:02:01〆 Struggle, little youth °998

string is a C++ style string, and string is essentially a class.
The difference between string and char*: char* is a pointer.String is a class, which encapsulates char*, manages this string, and is a char* container.
Features: The string class encapsulates a lot of member methods, such as find, delete, insert.
string manages the memory allocated by char*, don't worry about copying out of bounds and value out of bounds, etc.Responsible.
string constructor:
string();//Create an empty string, such as string str;
string(const char*s);//Initialize with string s
string(const string& str);//Initialize one string object with another string object
string(int n,char c);//Initialize with n characters c

String assignment operation:
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);//The character is assigned to the current string
string& assign(const char* s);//The string s is assigned to the current string
string& assign(const char* s,int n);//The characterAssign 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);//Assign n characters c to the current string

string concatenation:
string& operator+=(const char* str);//overload += operand
string& operator+=(const char c);//overload += operand
string& operator+=(const string& str);//Overload += operand
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 the 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);//N characters starting from pos in string s are connected to charactersend of string
string container string lookup:
int find(const string& str,int pos=0) const;//Find the first occurrence of str, start from pos
int find(const char c,int pos=0) const;//Find the first occurrence of character c
string& replace(int pos,int n,const string& str);//Replace n characters starting from pos to string str

String container string length comparison:
String type data can be compared directly, or function comparison can be called

string container character access:
char& operator[](int n);//through the [] methodGet character
char& at(int n);//Get character by at method

string container insertion and deletion:
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);//inDelete n at the specified position

stringContainer substring acquisition
string substr(int pos=0, int n);//return n strings starting from pos








copyright notice
author[〆 Struggle, little youth °998],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/218/202208061856482871.html

Random recommended