ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] # 简介 string封装了`char*`,管理这个字符串,是一个`char*`型的容器 # 常用操作 ## string构造函数 ~~~ string();//创建一个空的字符串 例如: string str; string(conststring& str);//使用一个string对象初始化另一个string对象 string(constchar* s);//使用字符串s初始化 string(int n, char c);//使用n个字符c初始化 ~~~ ~~~ string s1; string s2(10, 'a'); string s3(s2); string s4("hello"); cout << s1 << endl; cout << s2 << endl; cout << s3 << endl; cout << s4 << endl; ~~~ ## 基本赋值操作 ~~~ string&operator=(constchar* s);//char*类型字符串 赋值给当前的字符串 string&operator=(conststring&s);//把字符串s赋给当前的字符串 string&operator=(char c);//字符赋值给当前的字符串 string& assign(constchar *s);//把字符串s赋给当前的字符串 string& assign(constchar *s, int n);//把字符串s的前n个字符赋给当前的字符串 string& assign(conststring&s);//把字符串s赋给当前字符串 string& assign(int n, char c);//用n个字符c赋给当前字符串 string& assign(conststring&s, int start, int n);//将s从start开始n个字符赋值给字符串,如s=hello,那么n=3,start=1,那么是hel中从e开始赋值3-1个字符 ~~~ ~~~ string s1; s1 = "hello"; cout << s1 << endl; string s2; s2.assign("world", 2); cout << s2 << endl; ~~~ ## string存取字符操作 []和at的区别:[]访问元素时,越界不抛异常,直接挂,at越界,会抛异常 ~~~ char&operator[](int n);//通过[]方式取字符 char& at(int n);//通过at方法获取字符 ~~~ ~~~ string s = "hello world"; for (int i = 0; i < s.size(); ++i) { cout << s[i] << " "; } cout << endl; for (int j = 0; j < s.size(); ++j) { cout << s.at(j) << endl; } ~~~ ~~~ //[]和at的区别:[]访问元素时,越界不抛异常,直接挂,at越界,会抛异常 try { //cout << s[100] << endl; cout << s.at(100) << endl; } catch (out_of_range &ex) { cout << ex.what() << endl; cout << "at越界" << endl; } ~~~ ## 拼接操作 ~~~ string&operator+=(conststring& str);//重载+=操作符 string&operator+=(constchar* str);//重载+=操作符 string&operator+=(constchar c);//重载+=操作符 string& append(constchar *s);//把字符串s连接到当前字符串结尾 string& append(constchar *s, int n);//把字符串s的前n个字符连接到当前字符串结尾 string& append(conststring&s);//同operator+=() string& append(conststring&s, int pos, int n);//把字符串s中从pos开始的n个字符连接到当前字符串结尾 string& append(int n, char c);//在当前字符串结尾添加n个字符c ~~~ ~~~ string s1 = "aaa"; s1 += "bbb"; s1 += 'c'; cout << s1 << endl; s1.append("ddddddd", 3); cout << s1 << endl; ~~~ ## 查找和替换 ~~~ int find(const string& str, int pos = 0) const; //查找str第一次出现位置,从pos开始查找 int find(constchar* s, int pos = 0) const; //查找s第一次出现位置,从pos开始查找 int find(constchar* s, int pos, int n) const; //从pos位置查找s的前n个字符第一次位置 int find(constchar c, int pos = 0) const; //查找字符c第一次出现位置 int rfind(conststring& str, int pos = npos) const;//查找str最后一次位置,从pos开始查找 int rfind(constchar* s, int pos = npos) const;//查找s最后一次出现位置,从pos开始查找 int rfind(constchar* s, int pos, int n) const;//从pos查找s的前n个字符最后一次位置 int rfind(constchar c, int pos = 0) const; //查找字符c最后一次出现位置 string& replace(int pos, int n, const string& str); //替换从pos开始n个字符为字符串str string& replace(int pos, int n, const char* s); //替换从pos开始的n个字符为字符串s ~~~ ~~~ string s = "abcdefgd"; cout << s.find('d') << endl;//3 cout << s.rfind('d') << endl;//7 cout << s.find("kkk") << endl; s.replace(2, 4, "AAA"); cout << s << endl; ~~~ ## 比较操作 ~~~ /* compare函数在>时返回 1,<时返回 -1,==时返回 0。 比较区分大小写,比较时参考字典顺序,排越前面的越小。 大写的A比小写的a小。 */ int compare(conststring&s) const;//与字符串s比较 int compare(constchar *s) const;//与字符串s比较 ~~~ ~~~ string s1 = "hello"; string s2 = "hello"; const char* str = "world"; if (s1.compare(s2)==0) { cout << "s1==s2" << endl; } ~~~ ## 子串 ~~~ string substr(int pos = 0, int n = npos) const;//返回由pos开始的n个字符组成的字符串 ~~~ ~~~ string email = "hello world@itcast.com"; int pos = email.find('@'); string username = email.substr(0, pos); cout << username << endl; string prex = email.substr(pos + 1); cout << prex << endl; ~~~ ## 插入和删除 ~~~ string& insert(int pos, constchar* s); //插入字符串 string& insert(int pos, conststring& str); //插入字符串 string& insert(int pos, int n, char c);//在指定位置插入n个字符c string& erase(int pos, int n = npos);//删除从Pos开始的n个字符 ~~~ ~~~ string s = "aaaa"; s.insert(3, "AAA"); cout << s << endl; s.insert(3, 5, 'M'); cout << s << endl; s.erase(2, 3); cout << s << endl; ~~~ ## string和c-style字符串转换 ~~~ const char *str = "hello"; string s=string(str); cout << s << endl; const char *str2=s.c_str(); cout << str2 << endl; ~~~ 在c++中存在一个从const char\*到string的隐式类型转换,却不存在从一个string对象到C\_string的自动类型转换。对于string类型的字符串,可以通过c\_str()函数返回string对象对应的C\_string. 通常,程序员在整个程序中应坚持使用string类对象,直到必须将内容转化为char\*时才将其转换为C\_string. ## 迭代器 ~~~ string s = "hello"; for (string::iterator it = s.begin(); it != s.end(); ++it) { cout << *it << " "; } cout << endl; //反向遍历 for (string::reverse_iterator it = s.rbegin(); it != s.rend(); ++it) { cout << *it << " "; } cout << endl; ~~~ ## 注意 为了修改string字符串的内容,下标操作符\[\]和at都会返回字符的引用。但当字符串的内存被重新分配之后,可能发生错误. 原空间被释放,但是a还是被释放的s[2]空间的别名,如果操作非法的空间,会出错 ~~~ string s = "abcde"; char &a = s[2]; char &b = s[3]; a = '1'; b = '2'; cout << "a:" << a << endl; cout << "b:" << b << endl; cout << s << endl; cout << "字符串的原空间地址:" << (int*)s.c_str() << endl; s = "fdasfdasfdsafdasherewrkewhsaferew"; cout << "字符串的空间地址:" << (int*)s.c_str() << endl; ~~~