Home >Backend Development >C#.Net Tutorial >Summary of c++ review points z-12 - STL string

Summary of c++ review points z-12 - STL string

黄舟
黄舟Original
2017-01-16 11:55:571391browse

1String concept

² String is the string type of STL, usually used to represent strings. Before using string, strings were usually represented by char*. Both string and char* can be used to represent strings, so what is the difference between the two.

Comparison of string and char*

² String is a class, char* is a pointer to a character.

string encapsulates char* and manages this string as a char* type container.

² String does not need to consider memory release and out of bounds.

string manages the memory allocated by char*. Every time a string is copied, the value is maintained by the string class. There is no need to worry about copying out of bounds or value out of bounds.

² string provides a series of string operation functions (this will be discussed in detail later)

Find, copy, delete, erase, replace, and insert

2string constructor

² Default constructor:

string(); //Construct an empty string string s1.

² Copy constructor:

string(const string &str); //Construct a string that is the same as str. Such as strings1(s2).

² Constructor with parameters

string(const char *s); //Initialize with string s

string(int n,char c); // Initialize with n characters c

3 string access character operation

² String class character operation:

const char &operator[] (int n) const;
const char &at(int n) const;
char &operator[] (int n);
char &at(int n);

² operator[] and at() both return the nth character in the current string, but there is a difference between them.

The main difference is that at() will throw an exception when it goes out of bounds, [] will return (char)0 when it happens to go out of bounds, and when it continues to go out of bounds, the compiler will directly make an error. If your program hopes to catch exceptions through try and catch, it is recommended to use at().

4 Operation to obtain const char* from string

² const char *c_str() const; //Return a string ending with '\0' First address

5 Operation of copying string to the memory space pointed to by char*

² int copy(char *s, int n, int pos=0) const ;

Copy n characters starting with pos in the current string to the character array starting with s, and return the actual number of copies. Be careful to ensure that the space pointed to by s is large enough to accommodate the current string, otherwise it will go out of bounds.

6The length of string

int length() const; //Return the length of the current string. The length does not include the '\0' at the end of the string.

bool empty() const; //Whether the current string is empty

7string assignment

string &operator=(const string&s);//把字符串s赋给当前的字符串
string &assign(const char *s); //把字符串s赋给当前的字符串
string &assign(const char *s, int n);//把字符串s的前n个字符赋给当前的字符串
string &assign(const string&s); //把字符串s赋给当前字符串
string &assign(int n,char c); //用n个字符c赋给当前字符串
string &assign(const string &s,intstart, int n); //把字符串s中从start开始的n个字符赋给当前字符串

8string string connection

string &operator+=(const string&s); //把字符串s连接到当前字符串结尾
string &operator+=(const char *s);//把字符串s连接到当前字符串结尾
string &append(const char *s); //把字符串s连接到当前字符串结尾
string &append(const char *s,intn); //把字符串s的前n个字符连接到当前字符串结尾
string &append(const string&s); //同operator+=()
string &append(const string &s,intpos, int n);//把字符串s中从pos开始的n个字符连接到当前字符串结尾
string &append(int n, char c); //在当前字符串结尾添加n个字符c

Comparison of 9string

int compare(const string &s)const; //与字符串s比较
int compare(const char *s) const; //与字符串s比较
compare函数在>时返回 1,<时返回 -1,==时返回 0。比较区分大小写,比较时参考字典顺序,排越前面的越小。大写的A比小写的a小。

Substring of 10string

string substr(int pos=0, int n=npos)const; //返回由pos开始的n个字符组成的子字符串

Find and replace of 11string

Find

int find(char c,int pos=0) const; //从pos开始查找字符c在当前字符串的位置 
int find(const char *s, int pos=0)const; //从pos开始查找字符串s在当前字符串的位置
int find(const string &s, int pos=0)const; //从pos开始查找字符串s在当前字符串中的位置
find函数如果查找不到,就返回-1
int rfind(char c, int pos=npos) const; //从pos开始从后向前查找字符c在当前字符串中的位置 
int rfind(const char *s, int pos=npos)const;
int rfind(const string &s, intpos=npos) const;
//rfind是反向查找的意思,如果查找不到, 返回-1



Replacement

string &replace(int pos, int n, constchar *s);//删除从pos开始的n个字符,然后在pos处插入串s
string &replace(int pos, int n, conststring &s); //删除从pos开始的n个字符,然后在pos处插入串s
void swap(string &s2); //交换当前字符串与s2的值
//4 字符串的查找和替换
void main25()
{
strings1 = "wbm hello wbm 111 wbm 222 wbm 333";
size_tindex = s1.find("wbm", 0);
cout<< "index: " << index; 
//求itcast出现的次数
size_toffindex = s1.find("wbm", 0);
while(offindex != string::npos)
{
cout<< "在下标index: " << offindex << "找到wbm\n";
offindex= offindex + 1;
offindex= s1.find("wbm", offindex);
}
//替换 
strings2 = "wbm hello wbm 111 wbm 222 wbm 333";
s2.replace(0,3, "wbm");
cout<< s2 << endl;
//求itcast出现的次数
offindex= s2.find("wbm", 0);
while(offindex != string::npos)
{
cout<< "在下标index: " << offindex << "找到wbm\n";
s2.replace(offindex,3, "WBM");
offindex= offindex + 1;
offindex= s1.find("wbm", offindex);
}
cout<< "替换以后的s2:" << s2 << endl; 
}

Interval deletion and insertion of 12String

string &insert(int pos, const char *s);
string &insert(int pos, const string&s);
//前两个函数在pos位置插入字符串s
string &insert(int pos, int n, charc); //在pos位置 插入n个字符c
string &erase(int pos=0, intn=npos); //删除pos开始的n个字符,返回修改后的字符串

13string algorithm related

void main27()
{
strings2 = "AAAbbb";
transform(s2.begin(),s2.end(), s2.begin(), toupper);
cout<< s2 << endl;
strings3 = "AAAbbb";
transform(s3.begin(),s3.end(), s3.begin(), tolower);
cout<< s3 << endl;
}

The above is a summary of the key points of c++ review Twelve - the content of STL string. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn