目录一、定义和初始化 string二、string 常用迭代器三、string 常用运算符四、string 常用成员函数1.size 和 length 成员函数2.find 成员函数3.rfind 成员函数4.find_first_of 成员函数5.find_first_not_of 成员函数6.find_last_of 成员函数7.find_last_not_of 成员函数8.empty 成员函数9.insert 成员函数10.erase 成员函数11.push_back 成员函数12.pop_back 成员函数13.replace 成员函数14.substr 成员函数15.c_str 成员函数16.at 成员函数五、用于 string 的全局函数1.getline 函数2.stod 函数3.stoi 函数4.stoull 函数5.to_string 函数六、基于范围 for 处理每个字符前言本文详细介绍了C中string类的使用方法主要包括1. 多种初始化方式空字符串、C风格字符串、重复字符等2. 迭代器类型及遍历方法3. 常用运算符重载赋值、连接、比较等4. 核心成员函数查找、插入、删除、替换等5. 全局函数类型转换、输入输出等6. 基于范围的for循环遍历。文章通过具体代码示例演示了string类的各种操作包括字符串处理、查找替换、类型转换等功能涵盖了string类的主要应用场景。一、定义和初始化 string在 C 中string是标准库中提供的字符串类定义在string头文件中位于std命名空间内。#include string using namespace std;初始化方式默认初始化创建一个空字符串string s1; // s1 是一个空字符串使用 C 风格字符串初始化string s2(Hello, World!); // s2 初始化为 Hello, World!使用字符串字面值初始化string s3 Hello, World!; // 等同于上面的方式使用另一个 string 对象初始化string s4(s2); // s4 是 s2 的副本 string s5 s2; // 同样是 s2 的副本使用部分字符串初始化string s6(Hello, World!, 5); // s6 初始化为 Hello前 5 个字符使用重复字符初始化string s7(10, a); // s7 初始化为 aaaaaaaaaa10 个 a使用迭代器范围初始化string s8(s2.begin(), s2.begin() 5); // s8 初始化为 Hello二、string 常用迭代器string类提供了多种迭代器类型用于遍历字符串中的字符迭代器含义s.begin()第一个元素的迭代器s.end()最后一个元素的下一个位置迭代器(尾后迭代器或尾迭代器)s.cbegin()第一个元素的常量迭代器(不修改元素内容)s.cend()尾后常量迭代器(不修改元素内容)s.rbegin()从后往前的第一个迭代器s.rend()从后往前的最后一个迭代器s.crbegin()从后往前的第一个常量迭代器s.crend()从后往前的最后一个常量迭代器使用示例int main() { string s(abcde); for (string::const_iterator i s.cbegin(); i ! s.cend(); i)//输出每个字符并加, cout *i ,; cout endl; for (auto i2 s.rbegin(); i2 ! s.rend(); i2)//逆序输出每个字符并加 空格 cout *i2 ; cout endl; for (auto i3 s.begin()2; i3 ! s.end(); i3)//把除前2个字符外的字母变成大写 *i3 toupper(*i3); //需要引用cctype文件,在vs2022中可以不引用,因为string中已经引用过该文件 cout s endl; return 0; }说明: toupper函数把小写字母转为大写字母,该函数是C语言函数,由于C兼容C也可以直接使用,但需要引用对应的头文件cctype。三、string 常用运算符string类重载了多种运算符方便字符串操作运算符含义s1s2把s2的值赋值给s1s1s2返回s1和s2连接后的结果s1s2判断是否相等,区分大小写s1!s2判断是否不相等,,,判断大小关系,利用字典序进行比较,区分大小写s1s2在s1的后面连接s2s1[i]返回s1第i个字符的引用,从0开始couts输出s到输出流coutcins从cin输入流读取字符串到s使用示例int main() { string s1(abcde); string s2; string s3; s2 s1; //把s1的内容复制给s2; s3 s1 s2;//s1和s2的内容连结在一起复制给s3 cout s1: s1 endl; cout s2: s2 endl; cout s3: s3 endl; if (s1 s2) //判断两个字符串的内容是否相等 cout s1s2 endl; if (s1 ! s2)//判断两个字符串的内容是否不相等 cout s1!s2 endl; if (s1 s3)//判断两个字符串的大小 cout s1s3 endl; else if (s1 s3) cout s1s3 endl; else cout s1s3 endl; s1 s2; //把s2的值连结到s1的末尾 cout s1: s1 endl; cout s2: s2 endl; s1[0] x; //通过下标访问s1的内容 cout s1: s1 endl; return 0; }四、string 常用成员函数string s 的成员函数含义s.empty()判断是否为空串s.size()返回s的字符个数s.length()返回s的字符个数s.find()查找字符或字符串s.rfind()从后往前查找s.find_first_of查找第一个在字符集中的字符s.find_first_not_of查找第一个不在字符集中的字符s.find_last_of查找最后一个在字符集中的字符s.find_last_not_of查找最后一个不在字符集中的字符s.insert()插入s.erase()删除字符或字符串s.push_back()尾插s.pop_back()尾删s.replace()替换s.substr()复制子串s.swap()字符串交换s.c_str()string转换成c风格字符串s.at()获取指定位置的引用1.size 和 length 成员函数功能返回字符串的长度字符个数参数无返回值字符串的长度类型为size_t区别两者功能相同length()是为了与早期 C 兼容size()是 STL 风格的命名string s Hello; cout s.size(); // 输出: 5 cout s.length(); // 输出: 52.find 成员函数功能在字符串中查找子串或字符的第一次出现位置参数str要查找的子串pos开始查找的位置默认为 0返回值找到的位置若未找到则返回string::nposstring s Hello, World!; size_t pos s.find(World); if (pos ! string::npos) { cout Found at position: pos; // 输出: Found at position: 7 }3.rfind 成员函数功能在字符串中查找子串或字符的最后一次出现位置参数str要查找的子串pos开始查找的位置默认为string::npos表示从字符串末尾开始返回值找到的位置若未找到则返回string::nposstring s Hello, World!; size_t pos s.rfind(o); if (pos ! string::npos) { cout Found at position: pos; // 输出: Found at position: 8 }4.find_first_of 成员函数功能在字符串中查找第一个属于指定字符集的字符参数str包含要查找的字符集的字符串pos开始查找的位置默认为 0返回值找到的位置若未找到则返回string::nposstring s Hello, World!; size_t pos s.find_first_of(aeiou); if (pos ! string::npos) { cout First vowel at position: pos; // 输出: First vowel at position: 1 }5.find_first_not_of 成员函数功能在字符串中查找第一个不属于指定字符集的字符参数str包含要排除的字符集的字符串pos开始查找的位置默认为 0返回值找到的位置若未找到则返回string::nposstring s Hello, World!; size_t pos s.find_first_not_of(Helo); if (pos ! string::npos) { cout First non-Helo character at position: pos; // 输出: First non-Helo character at position: 5 }6.find_last_of 成员函数功能在字符串中查找最后一个属于指定字符集的字符参数str包含要查找的字符集的字符串pos开始查找的位置默认为string::npos表示从字符串末尾开始返回值找到的位置若未找到则返回string::nposstring s Hello, World!; size_t pos s.find_last_of(aeiou); if (pos ! string::npos) { cout Last vowel at position: pos; // 输出: Last vowel at position: 8 }7.find_last_not_of 成员函数功能在字符串中查找最后一个不属于指定字符集的字符参数str包含要排除的字符集的字符串pos开始查找的位置默认为string::npos表示从字符串末尾开始返回值找到的位置若未找到则返回string::nposstring s Hello, World!; size_t pos s.find_last_not_of(!); if (pos ! string::npos) { cout Last non-! character at position: pos; // 输出: Last non-! character at position: 11 }8.empty 成员函数功能判断字符串是否为空参数无返回值如果字符串为空返回true否则返回falsestring s1; string s2 Hello; cout s1.empty(); // 输出: 1 (true) cout s2.empty(); // 输出: 0 (false)9.insert 成员函数功能在指定位置插入字符串或字符参数pos插入位置str要插入的字符串返回值插入后的字符串引用string s Hello World; s.insert(5, ,); // s 现在是 Hello, World10.erase 成员函数功能删除字符串中的部分字符参数pos开始删除的位置默认为 0len要删除的字符数默认为string::npos表示删除到字符串末尾返回值删除后的字符串引用string s Hello, World!; s.erase(5, 2); // s 现在是 HelloWorld!11.push_back 成员函数功能在字符串末尾添加一个字符参数c要添加的字符返回值无string s Hello; s.push_back(!); // s 现在是 Hello!12.pop_back 成员函数功能删除字符串末尾的一个字符参数无返回值无string s Hello!; s.pop_back(); // s 现在是 Hello13.replace 成员函数功能替换字符串中的部分字符参数pos开始替换的位置len要替换的字符数str替换的字符串返回值替换后的字符串引用string s Hello, World!; s.replace(7, 5, C); // s 现在是 Hello, C!14.substr 成员函数功能返回字符串的子串参数pos子串的起始位置len子串的长度默认为string::npos表示到字符串末尾返回值子串string s Hello, World!; string sub s.substr(7, 5); // sub 是 World15.swap 成员函数功能交换两个字符串的内容参数str要交换的另一个字符串返回值无string s1 Hello; string s2 World; s1.swap(s2); // s1 现在是 Worlds2 现在是 Hello15.c_str 成员函数功能返回一个指向 C 风格字符串的指针以 null 结尾参数无返回值指向 C 风格字符串的指针string s Hello; const char* cstr s.c_str(); cout cstr; // 输出: Hello16.at 成员函数功能访问指定位置的字符进行边界检查参数pos字符的位置返回值指定位置的字符的引用异常如果位置超出范围抛出out_of_range异常string s Hello; char c s.at(0); // c 是 H try { c s.at(10); // 抛出 out_of_range 异常 } catch (const out_of_range e) { cout Exception: e.what(); }五、用于 string 的全局函数下面的函数都不是string的成员函数,而是用于string比较常用的全局函数。全局函数作用getline从输入流读取一行字符stod将字符序列转换为 doublestoi将字符序列转换为intstoull将字符序列转换为unsigned long longto_string把数字转成对应的字符串1.getline 函数功能从输入流中读取一行字符串直到遇到换行符参数is输入流对象str存储读取结果的字符串返回值输入流对象的引用string s; cout Enter a line: ; getline(cin, s); cout You entered: s;2.stod 函数功能将字符串转换为 double 类型参数str要转换的字符串idx可选参数用于存储转换后第一个未使用字符的位置返回值转换后的 double 值string s 3.14159; double d stod(s); cout d; // 输出: 3.141593.stoi 函数功能将字符串转换为 int 类型参数str要转换的字符串idx可选参数用于存储转换后第一个未使用字符的位置base可选参数转换的基数默认为 10返回值转换后的 int 值string s 123; int i stoi(s); cout i; // 输出: 1234.stoull 函数功能将字符串转换为 unsigned long long 类型参数str要转换的字符串idx可选参数用于存储转换后第一个未使用字符的位置base可选参数转换的基数默认为 10返回值转换后的 unsigned long long 值string s 123456789012345; unsigned long long ull stoull(s); cout ull; // 输出: 1234567890123455.to_string 函数功能将数值转换为字符串参数val要转换的数值可以是 int、long、long long、unsigned int、unsigned long、unsigned long long、float、double 或 long double返回值转换后的字符串int i 123; string s to_string(i); cout s; // 输出: 123六、基于范围 for 处理每个字符C11 引入了基于范围的 for 循环可以更简洁地遍历字符串中的每个字符语法for (char c : str) { // 处理字符 c }示例string s(hello); //输出 for (auto x : s) //对于s中的每一个字符 cout x ;//输出当前字符,后面紧跟一个空格 // 修改字符串中的字符 for (auto c : s) //对于s中的每个字符(注意:c是引用) c toupper(c);//c是引用,因此赋值将改变s中字符的值 cout s endl;注意事项如果只是读取字符使用auto c如果需要修改字符使用auto c基于范围的 for 循环会自动遍历字符串中的每个字符不需要手动管理索引或迭代器
C++ String 详解
发布时间:2026/6/4 21:21:04
目录一、定义和初始化 string二、string 常用迭代器三、string 常用运算符四、string 常用成员函数1.size 和 length 成员函数2.find 成员函数3.rfind 成员函数4.find_first_of 成员函数5.find_first_not_of 成员函数6.find_last_of 成员函数7.find_last_not_of 成员函数8.empty 成员函数9.insert 成员函数10.erase 成员函数11.push_back 成员函数12.pop_back 成员函数13.replace 成员函数14.substr 成员函数15.c_str 成员函数16.at 成员函数五、用于 string 的全局函数1.getline 函数2.stod 函数3.stoi 函数4.stoull 函数5.to_string 函数六、基于范围 for 处理每个字符前言本文详细介绍了C中string类的使用方法主要包括1. 多种初始化方式空字符串、C风格字符串、重复字符等2. 迭代器类型及遍历方法3. 常用运算符重载赋值、连接、比较等4. 核心成员函数查找、插入、删除、替换等5. 全局函数类型转换、输入输出等6. 基于范围的for循环遍历。文章通过具体代码示例演示了string类的各种操作包括字符串处理、查找替换、类型转换等功能涵盖了string类的主要应用场景。一、定义和初始化 string在 C 中string是标准库中提供的字符串类定义在string头文件中位于std命名空间内。#include string using namespace std;初始化方式默认初始化创建一个空字符串string s1; // s1 是一个空字符串使用 C 风格字符串初始化string s2(Hello, World!); // s2 初始化为 Hello, World!使用字符串字面值初始化string s3 Hello, World!; // 等同于上面的方式使用另一个 string 对象初始化string s4(s2); // s4 是 s2 的副本 string s5 s2; // 同样是 s2 的副本使用部分字符串初始化string s6(Hello, World!, 5); // s6 初始化为 Hello前 5 个字符使用重复字符初始化string s7(10, a); // s7 初始化为 aaaaaaaaaa10 个 a使用迭代器范围初始化string s8(s2.begin(), s2.begin() 5); // s8 初始化为 Hello二、string 常用迭代器string类提供了多种迭代器类型用于遍历字符串中的字符迭代器含义s.begin()第一个元素的迭代器s.end()最后一个元素的下一个位置迭代器(尾后迭代器或尾迭代器)s.cbegin()第一个元素的常量迭代器(不修改元素内容)s.cend()尾后常量迭代器(不修改元素内容)s.rbegin()从后往前的第一个迭代器s.rend()从后往前的最后一个迭代器s.crbegin()从后往前的第一个常量迭代器s.crend()从后往前的最后一个常量迭代器使用示例int main() { string s(abcde); for (string::const_iterator i s.cbegin(); i ! s.cend(); i)//输出每个字符并加, cout *i ,; cout endl; for (auto i2 s.rbegin(); i2 ! s.rend(); i2)//逆序输出每个字符并加 空格 cout *i2 ; cout endl; for (auto i3 s.begin()2; i3 ! s.end(); i3)//把除前2个字符外的字母变成大写 *i3 toupper(*i3); //需要引用cctype文件,在vs2022中可以不引用,因为string中已经引用过该文件 cout s endl; return 0; }说明: toupper函数把小写字母转为大写字母,该函数是C语言函数,由于C兼容C也可以直接使用,但需要引用对应的头文件cctype。三、string 常用运算符string类重载了多种运算符方便字符串操作运算符含义s1s2把s2的值赋值给s1s1s2返回s1和s2连接后的结果s1s2判断是否相等,区分大小写s1!s2判断是否不相等,,,判断大小关系,利用字典序进行比较,区分大小写s1s2在s1的后面连接s2s1[i]返回s1第i个字符的引用,从0开始couts输出s到输出流coutcins从cin输入流读取字符串到s使用示例int main() { string s1(abcde); string s2; string s3; s2 s1; //把s1的内容复制给s2; s3 s1 s2;//s1和s2的内容连结在一起复制给s3 cout s1: s1 endl; cout s2: s2 endl; cout s3: s3 endl; if (s1 s2) //判断两个字符串的内容是否相等 cout s1s2 endl; if (s1 ! s2)//判断两个字符串的内容是否不相等 cout s1!s2 endl; if (s1 s3)//判断两个字符串的大小 cout s1s3 endl; else if (s1 s3) cout s1s3 endl; else cout s1s3 endl; s1 s2; //把s2的值连结到s1的末尾 cout s1: s1 endl; cout s2: s2 endl; s1[0] x; //通过下标访问s1的内容 cout s1: s1 endl; return 0; }四、string 常用成员函数string s 的成员函数含义s.empty()判断是否为空串s.size()返回s的字符个数s.length()返回s的字符个数s.find()查找字符或字符串s.rfind()从后往前查找s.find_first_of查找第一个在字符集中的字符s.find_first_not_of查找第一个不在字符集中的字符s.find_last_of查找最后一个在字符集中的字符s.find_last_not_of查找最后一个不在字符集中的字符s.insert()插入s.erase()删除字符或字符串s.push_back()尾插s.pop_back()尾删s.replace()替换s.substr()复制子串s.swap()字符串交换s.c_str()string转换成c风格字符串s.at()获取指定位置的引用1.size 和 length 成员函数功能返回字符串的长度字符个数参数无返回值字符串的长度类型为size_t区别两者功能相同length()是为了与早期 C 兼容size()是 STL 风格的命名string s Hello; cout s.size(); // 输出: 5 cout s.length(); // 输出: 52.find 成员函数功能在字符串中查找子串或字符的第一次出现位置参数str要查找的子串pos开始查找的位置默认为 0返回值找到的位置若未找到则返回string::nposstring s Hello, World!; size_t pos s.find(World); if (pos ! string::npos) { cout Found at position: pos; // 输出: Found at position: 7 }3.rfind 成员函数功能在字符串中查找子串或字符的最后一次出现位置参数str要查找的子串pos开始查找的位置默认为string::npos表示从字符串末尾开始返回值找到的位置若未找到则返回string::nposstring s Hello, World!; size_t pos s.rfind(o); if (pos ! string::npos) { cout Found at position: pos; // 输出: Found at position: 8 }4.find_first_of 成员函数功能在字符串中查找第一个属于指定字符集的字符参数str包含要查找的字符集的字符串pos开始查找的位置默认为 0返回值找到的位置若未找到则返回string::nposstring s Hello, World!; size_t pos s.find_first_of(aeiou); if (pos ! string::npos) { cout First vowel at position: pos; // 输出: First vowel at position: 1 }5.find_first_not_of 成员函数功能在字符串中查找第一个不属于指定字符集的字符参数str包含要排除的字符集的字符串pos开始查找的位置默认为 0返回值找到的位置若未找到则返回string::nposstring s Hello, World!; size_t pos s.find_first_not_of(Helo); if (pos ! string::npos) { cout First non-Helo character at position: pos; // 输出: First non-Helo character at position: 5 }6.find_last_of 成员函数功能在字符串中查找最后一个属于指定字符集的字符参数str包含要查找的字符集的字符串pos开始查找的位置默认为string::npos表示从字符串末尾开始返回值找到的位置若未找到则返回string::nposstring s Hello, World!; size_t pos s.find_last_of(aeiou); if (pos ! string::npos) { cout Last vowel at position: pos; // 输出: Last vowel at position: 8 }7.find_last_not_of 成员函数功能在字符串中查找最后一个不属于指定字符集的字符参数str包含要排除的字符集的字符串pos开始查找的位置默认为string::npos表示从字符串末尾开始返回值找到的位置若未找到则返回string::nposstring s Hello, World!; size_t pos s.find_last_not_of(!); if (pos ! string::npos) { cout Last non-! character at position: pos; // 输出: Last non-! character at position: 11 }8.empty 成员函数功能判断字符串是否为空参数无返回值如果字符串为空返回true否则返回falsestring s1; string s2 Hello; cout s1.empty(); // 输出: 1 (true) cout s2.empty(); // 输出: 0 (false)9.insert 成员函数功能在指定位置插入字符串或字符参数pos插入位置str要插入的字符串返回值插入后的字符串引用string s Hello World; s.insert(5, ,); // s 现在是 Hello, World10.erase 成员函数功能删除字符串中的部分字符参数pos开始删除的位置默认为 0len要删除的字符数默认为string::npos表示删除到字符串末尾返回值删除后的字符串引用string s Hello, World!; s.erase(5, 2); // s 现在是 HelloWorld!11.push_back 成员函数功能在字符串末尾添加一个字符参数c要添加的字符返回值无string s Hello; s.push_back(!); // s 现在是 Hello!12.pop_back 成员函数功能删除字符串末尾的一个字符参数无返回值无string s Hello!; s.pop_back(); // s 现在是 Hello13.replace 成员函数功能替换字符串中的部分字符参数pos开始替换的位置len要替换的字符数str替换的字符串返回值替换后的字符串引用string s Hello, World!; s.replace(7, 5, C); // s 现在是 Hello, C!14.substr 成员函数功能返回字符串的子串参数pos子串的起始位置len子串的长度默认为string::npos表示到字符串末尾返回值子串string s Hello, World!; string sub s.substr(7, 5); // sub 是 World15.swap 成员函数功能交换两个字符串的内容参数str要交换的另一个字符串返回值无string s1 Hello; string s2 World; s1.swap(s2); // s1 现在是 Worlds2 现在是 Hello15.c_str 成员函数功能返回一个指向 C 风格字符串的指针以 null 结尾参数无返回值指向 C 风格字符串的指针string s Hello; const char* cstr s.c_str(); cout cstr; // 输出: Hello16.at 成员函数功能访问指定位置的字符进行边界检查参数pos字符的位置返回值指定位置的字符的引用异常如果位置超出范围抛出out_of_range异常string s Hello; char c s.at(0); // c 是 H try { c s.at(10); // 抛出 out_of_range 异常 } catch (const out_of_range e) { cout Exception: e.what(); }五、用于 string 的全局函数下面的函数都不是string的成员函数,而是用于string比较常用的全局函数。全局函数作用getline从输入流读取一行字符stod将字符序列转换为 doublestoi将字符序列转换为intstoull将字符序列转换为unsigned long longto_string把数字转成对应的字符串1.getline 函数功能从输入流中读取一行字符串直到遇到换行符参数is输入流对象str存储读取结果的字符串返回值输入流对象的引用string s; cout Enter a line: ; getline(cin, s); cout You entered: s;2.stod 函数功能将字符串转换为 double 类型参数str要转换的字符串idx可选参数用于存储转换后第一个未使用字符的位置返回值转换后的 double 值string s 3.14159; double d stod(s); cout d; // 输出: 3.141593.stoi 函数功能将字符串转换为 int 类型参数str要转换的字符串idx可选参数用于存储转换后第一个未使用字符的位置base可选参数转换的基数默认为 10返回值转换后的 int 值string s 123; int i stoi(s); cout i; // 输出: 1234.stoull 函数功能将字符串转换为 unsigned long long 类型参数str要转换的字符串idx可选参数用于存储转换后第一个未使用字符的位置base可选参数转换的基数默认为 10返回值转换后的 unsigned long long 值string s 123456789012345; unsigned long long ull stoull(s); cout ull; // 输出: 1234567890123455.to_string 函数功能将数值转换为字符串参数val要转换的数值可以是 int、long、long long、unsigned int、unsigned long、unsigned long long、float、double 或 long double返回值转换后的字符串int i 123; string s to_string(i); cout s; // 输出: 123六、基于范围 for 处理每个字符C11 引入了基于范围的 for 循环可以更简洁地遍历字符串中的每个字符语法for (char c : str) { // 处理字符 c }示例string s(hello); //输出 for (auto x : s) //对于s中的每一个字符 cout x ;//输出当前字符,后面紧跟一个空格 // 修改字符串中的字符 for (auto c : s) //对于s中的每个字符(注意:c是引用) c toupper(c);//c是引用,因此赋值将改变s中字符的值 cout s endl;注意事项如果只是读取字符使用auto c如果需要修改字符使用auto c基于范围的 for 循环会自动遍历字符串中的每个字符不需要手动管理索引或迭代器