C 标准库提供了std::string类位于string头文件包含丰富的成员函数用于字符串的创建、访问、修改、查找、比较等操作。下面按功能分类介绍常用函数及其用法。注意以下函数均以std::string对象调用。若需要 C 风格字符串char*函数如strlen、strcpy需包含cstring但它们不是std::string的成员函数不在此详述。1. 构造与赋值函数/操作说明示例string()默认构造空字符串string s;string(const char*)从 C 字符串构造string s(hello);string(size_t, char)构造重复 n 次字符 c 的字符串string s(5, A);// AAAAAstring(const string)拷贝构造string s2(s1);operator赋值支持char*、string、chars abc; s s2; s X;assign()更灵活的赋值可指定长度或范围s.assign(3, B);// BBB#include string using namespace std; string s1; // 空字符串 string s2 world; // 从 C 字符串构造 string s3(s2); // 拷贝构造 string s4(3, x); // xxx s1 hello; // 赋值 s1.assign(4, a); // aaaa2. 访问元素函数/操作说明示例operator[]下标访问不检查边界char c s[0]; s[1] b;at()下标访问检查边界越界抛异常char c s.at(0);front()返回首字符引用char c s.front();back()返回末字符引用s.back() z;data()/c_str()返回底层 C 风格字符串只读printf(%s, s.c_str());string s test; cout s[0]; // t cout s.at(1); // e s.front() T; // 变为 Test s.back() t; // 变为 Test (末尾 t) const char* p s.c_str(); // 指向 Test3. 容量相关函数说明示例size()/length()返回字符个数size_t len s.size();empty()是否为空if (s.empty()) ...capacity()当前已分配的内存可容纳字符数size_t cap s.capacity();reserve(n)预留至少 n 个字符的空间s.reserve(100);resize(n, c)将字符串长度改为 n多出的位置填充 c默认\0s.resize(10, ?);shrink_to_fit()C11释放多余容量s.shrink_to_fit();4. 修改字符串函数/操作说明示例operator/append追加字符串或字符s !; s.append(3, !);push_back(c)追加字符 cs.push_back(!);pop_back()(C11)删除最后一个字符s.pop_back();insert(pos, str)在位置 pos 前插入 strs.insert(2, XYZ);erase(pos, len)从 pos 删除 len 个字符默认删除到末尾s.erase(3, 2);replace(pos, len, str)替换从 pos 开始 len 个字符为 strs.replace(0, 2, good);clear()清空所有字符s.clear();substr(pos, len)返回子串从 pos 开始长度 len默认到结尾string sub s.substr(2, 4);string s Hello; s world; // Hello world s.append(2, !); // Hello world!! s.insert(6, beautiful ); // Hello beautiful world!! s.erase(5, 10); // 删除 beautiful - Helloworld!! s.replace(5, 5, C); // Hello C!! string sub s.substr(6, 3); // C5. 查找与比较函数说明示例find(str, pos)从 pos 开始查找子串 str返回首次位置未找到返回string::npossize_t p s.find(lo);rfind(str, pos)反向查找从右向左p s.rfind(l);find_first_of(str, pos)查找 str 中任意字符第一次出现的位置p s.find_first_of(aeiou);find_last_of(str, pos)查找 str 中任意字符最后一次出现的位置find_first_not_of(str)查找第一个不属于 str 的字符find_last_not_of(str)查找最后一个不属于 str 的字符compare(str)与 str 比较返回 0相等、0小于、0大于if (s.compare(abc) 0)string s Hello world; size_t pos s.find(world); // 6 if (pos ! string::npos) { cout found at pos; } pos s.find_first_of(aeiou); // 1 (e) pos s.find_last_of(aeiou); // 7 (o) int cmp s.compare(Hello); // 0因为 Hello world Hello6. 迭代器std::string提供随机访问迭代器可用于遍历或配合 STL 算法。函数说明begin()/end()正向迭代器rbegin()/rend()反向迭代器cbegin()/cend()(C11)常量正向迭代器string s abc; for (auto it s.begin(); it ! s.end(); it) cout *it; // abc for (char c : s) cout c; // 范围 for 更简洁7. 辅助函数非成员函数但常用函数/操作说明示例operator连接两个字符串或字符串与字符string s a b;注意避免字面量直接相加getline(is, s, delim)从输入流读取一行到字符串getline(cin, s);to_string(val)将数值转换为字符串C11string s to_string(123);stoi(s)/stol/stod...将字符串转换为数值int i stoi(42);string s1 Hello, s2 C; string s3 s1 s2; // Hello C string line; getline(cin, line); // 读取一行含空格 string numStr to_string(3.14); // 3.140000 double pi stod(3.14159); // 3.14159完整示例#include iostream #include string using namespace std; int main() { string s Hello, C; // 大小与访问 cout Length: s.size() endl; // 10 cout First char: s.front() endl; // H cout Last char: s.back() endl; // // 修改 s.insert(5, world); // Hello world, C s.erase(5, 6); // 删除 world - Hello, C s.replace(7, 3, Python); // Hello, Python // 查找 size_t pos s.find(Python); if (pos ! string::npos) cout Found at pos endl; // 7 // 子串 string sub s.substr(0, 5); // Hello // 比较 if (sub Hello) cout Equal endl; // 转换 string num to_string(2025); int year stoi(num); // 遍历 for (char ch : s) cout ch ; cout endl; return 0; }提示使用string::npos通常是size_t(-1)作为查找失败的返回值。多数函数参数支持const stringconst char*单个字符char部分支持initializer_list或迭代器范围。若需要高效修改且可能涉及大量字符拼接可预先reserve()以减少内存重分配。C11 起data()与c_str()返回的字符数组末尾有\0可直接传给 C 函数。以上是std::string最常用的成员函数及用法。如果需要更详细的参考可查阅 cppreference.com - std::basic_string。----------------------------点个赞吧谢谢