C++ string 容器完全指南

张开发
2026/5/4 13:22:13 15 分钟阅读
C++ string 容器完全指南
C string 容器完全指南从入门到精通告别 C 风格字符串的繁琐拥抱std::string的强大与安全前言在 C 中std::string是一个专门用于处理字符串的容器它封装了字符数组提供了丰富的成员函数让字符串操作变得简单、安全且高效。相比于 C 语言的char*和string.h函数std::string自动管理内存、支持动态扩展并且兼容 STL 算法。本文将全面总结std::string的各种用法并提供大量可运行的示例代码帮助你快速掌握这一核心工具。一. 准备工作使用std::string需要包含头文件string命名空间为std。#include iostream #include string using namespace std; * 或者使用 std::string二. 构造函数构造函数多种方式创建字符串* 默认无参构造 * string(); * 用 count 个字符 ch 构造 * string(size_type count, CharT ch); * 用 s 字符串构造 * string(const CharT* s); * 取 s 字符串前 count 个字符构造 * string(const CharT* s, size_type count); * 取 s 字符串 pos 位置开始 count 个字符构造 * string(const CharT* s, size_type pos, size_type count); * 拷贝构造 * string(const string other); * 取 other 对象前 count 个字符构造 * string(const string other, size_type pos); * 取 other 对象pos位置开始 count 个字符构造 * string(const string other, size_type pos, size_type count); * 移动构造 * string(string other);示例#include iostream #include string using namespace std; int main() { string s1; * 空字符串 string s2(5, A); * AAAAA string s3(Hello); * Hello string s4(Hello World, 4); * Hell string s5(Hello World, 2, 5);* llo W string s6(s3); * 拷贝 Hello string s7(s3, 2); * 拷贝 He string s8(s3, 1, 3); * 拷贝 ell string s9(std::move(s3)); * 移动 Hello, s3为空 return 0; }三. 赋值操作3.1 赋值函数assign函数原型* 字符参数 * string assign(size_type count, CharT c); * 字符串参数 * string assign(const CharT* s); * string assign(const CharT* s, size_type count); * string assign(const CharT* s, size_type pos, size_type count); * string对象参数 * string assign(const string other); * string assign(const string other, size_type pos, size_type count npos);示例#include iostream #include string using namespace std; int main() { string str; str.assign(4, B); // BBBB str.assign(Hello World); // Hello World str.assign(Hello World, 4); // Hell str.assign(Hello World, 4, 5); // o Wor string another Target; str.assign(another); // Target str.assign(another, 2); // rget str.assign(another, 2, 3); // rge return 0; }3.2 赋值运算符operator函数原型string operator( CharT c ); string operator( const CharT* s ); string operator( std::initializer_listCharT list ); string operator( const string other); string operator( string other) templateclass StringViewLike string operator( const StringViewLike t );示例#include iostream #include string using namespace std; int main() { string str1; str1 c; // c str1 hello world; // hello world str1 {a,b,c,d};// abcd string str2 str1; // abcd str2 std::move(str1); // abcd str1空了 string_view str3 str2; // abcd str1 str3; // abcd return 0; }四. 字符串拼接4.1 结尾追加append函数原型* 追加 count 个字符 ch * string append(size_type count, CharT ch); * 追加字符串 * string append(const CharT* s); * string append(const CharT* s, size_type count); * string append(const CharT* s, size_type pos, size_type count); * 追加另一个对象 * string append(const string str); * string append(const string str, size_type pos, size_type count npos);示例#include iostream #include string using namespace std; int main() { string str Hello; str.append(3, !); // Hello!!! str.append( World); // Hello!!! World str.append( Chah, 4); // Hello!!! World C string tail Goodbye; str.append(tail); // Hello!!! World C Goodbye str.append(tail,1,4); // Hello!!! World C GoodbyeGood str.append(tail,5); // Hello!!! World C GoodbyeGoodbye return 0; }4.2 结尾追加字符push_back函数原型* void push_back( CharT ch );示例string s Hi; s.push_back(!); // Hi!4.3 拼接运算符operator函数原型* string对象拼接一段 * string operator( const string str ); * string operator( CharT ch ); * string operator( const CharT* s );示例string s Hello, r Good; s World; // Hello World s !; // Hello World! s C; // Hello World! C s r; // Hello World! C Good五. 插入与替换5.1 指定位置插入insert* 字符参数 * string insert(size_type index, size_type count, CharT ch); * 字符串参数 * string insert(size_type index, const CharT * s); * string insert(size_type index, const CharT * s, size_type count); * string insert(size_type index, const CharT * s, size_type pos, size_type count); * string对象参数 * string insert(size_type index, const string str); * string insert(size_type index, const string str, size_type pos, size_type count npos);示例#include iostream #include string using namespace std; int main() { string s HelloWorld; s.insert(5, 2, -); // Hello--World s.insert(5, ,); // Hello,--World s.insert(5, 12345, 3); // Hello123,--World s.insert(5, 56789, 1, 2); // Hello67123,--World string t abcdefg; s.insert(0, t); // abcdefgHello67123,--World s.insert(0, t, 5); // fgabcdefgHello67123,--World s.insert(0, t, 2, 3); // cdefgabcdefgHello67123,--World return 0; }5.2 替换子串replace函数原型* 字符参数 * string replace(size_type pos, size_type count, size_type count2, CharT ch); * 字符串参数 * string replace(size_type pos, size_type count,const CharT* cstr); * string replace(size_type pos, size_type count,const CharT* cstr, size_type count2); * string replace(size_type pos, size_type count, const CharT* cstr, size_type pos, size_type count); * string对象参数 * string replace(size_type pos, size_type count, const string str); * string replace(size_type pos, size_type count, const string str, size_type pos2, size_type count2 npos);示例#include iostream #include string using namespace std; int main() { string s abcdefghijklmn; s.replace(1, 2, 3, ,); // a,,,defghijklmn s.replace(1, 2, ???); // a???,defghijklmn s.replace(1, 2, 12345, 3); // a123?,defghijklmn s.replace(1, 2, 56789, 1, 2); // a673?,defghijklmn string t ,.;]{; s.replace(1, 2, t); // a,.;]{3?,defghijklmn s.replace(1, 2, t, 3); // a]{;]{3?,defghijklmn s.replace(1, 2, t, 2, 3); // a;]{;]{3?,defghijklmn return 0; }六. 删除操作6.1 清空字符串clear函数原型void clear();示例string s Hello World!; s.clear(); // 完全清空6.2 删除指定字符erase函数原型* 下标index位置开始删除count个字符 * string erase(size_type index 0, size_type count npos);示例string s Hello World!; s.erase(5, 6); // 从下标5开始删6个字符 - Hello! s Hello World!; s.erase(4); // 从下标4开始删除全部 - Hell s Hello World!; s.erase(); // 删除全部 - Hell6.3 删除最后一个字符pop_back函数原型void pop_back();示例string s Hello World!; s.pop_back(); // 删除 ! - Hello World七. 元素访问操作7.1 下标运算符[ ]函数原型* [] 下标访问返回元素引用 * CharT operator[]( size_type pos ); * const CharT operator[]( size_type pos ) const;示例string s Hello; cout s[1] endl; // e s[1] w; cout s endl; // hwllo s[10] r; // 越界崩溃中断7.2 指定元素引用at( )函数原型* 返回string对象指定下标的元素引用有边界检查 * CharT at( size_type pos ); * const CharT at( size_type pos ) const;示例string s Hello; cout s.at(1) endl; // e s.at(1) w; cout s endl; // hwllo s.at(10) r; // 越界异常中断7.3. 首字符引用front( )函数原型* front 返回对象首元素引用 * CharT front(); * const CharT front() const;示例string s Hello; cout s.front() endl; // H s.front() w; // wello7.4. 尾字符引用back( )函数原型* back 返回对象尾元素引用 * CharT back(); * const CharT back() const;示例string s Hello; cout s.back() endl; // o s.back() w; // Hellw7.5. 对象内字符串地址c_str( )函数原型* c_str 返回对象中字符串的地址const char* * const CharT* c_str() const;示例string s Hello World!; cout s endl; // Hello World! cout s.c_str() endl; // Hello World! const char* ptr s.c_str(); // 只读 cout ptr[1] endl; // e // ptr[1] t; //语法报错7.6. 对象内字符串地址data( )函数原型* data 返回string对象内字符串的地址 * const CharT* data() const; * CharT* data() noexcept;示例string s Hello World!; cout s endl; // Hello World! cout s.data() endl; // Hello World! char* ptr s.data(); // 可读可写 cout ptr[1] endl; // e ptr[1] t; cout s endl; // Htllo World!八. 容量与大小8.1 获取字符个数size( ) / length( )函数原型* 返回当前string对象中的元素个数 * size_type size() const; * size_type length() const;示例int main() { string s Hello World; cout s.size() endl; // 11 cout s.length() endl; // 11 return 0; }8.2 获取容量capacity( )容量 元素个数是最大容纳元素数初始15字节扩容16*n字节函数原型* 返回string对象的容量初始15扩容每次增加16*n不同环境不一样 * size_type capacity() const;示例int main() { string s Hello World; cout s.size() endl; // 11 cout s.capacity() endl; // 15 初始容量 s 123456789; cout s.size() endl; // 20 cout s.capacity() endl; // 31 扩容 16 return 0; }8.3 返回字符个数resize( )函数原型* resize 重新指定字符数量, 根据需要增加/删除元素 * void resize( size_type count ); * void resize( size_type count, CharT ch );示例int main() { string s Hello World; s.resize(20); // 扩大补\0 cout s -- s.size() endl; // Hello World -- 20 s.resize(3); // 缩小删掉多余 cout s -- s.size() endl; // Hel -- 3 return 0; }8.4 返回字符个数reserve( )函数原型* 将字符串的容量设置为至少与指定数字一样大的数字, 1516*n * void reserve( size_type new_cap );比如设置10容量则为15 16 * 0 15设置20容量则为15 16 * 1 31设置31容量则为15 16 * 1 31示例int main() { string s Hello World; s.reserve(20); cout s.capacity() endl; // 31 s.reserve(33); cout s.capacity() endl; // 47 s.reserve(10); // 缩小无效 cout s.capacity() endl; // 47 return 0; }8.5 返回字符个数empty( )函数原型* 判断string对象是否是空的空返回真非空返回假 * bool empty() const;示例int main() { string s1 Hello World; cout s1.empty() endl; // false 0 string s2; cout s2.empty() endl; // true 1 return 0; }8.6 返回字符个数shrink_to_fit( )函数原型* shrink_to_fit 丢弃字符串的多余容量剩余1516*n * void shrink_to_fit();对象内很多的字符删掉了很多闲空间比较多调用此函数示例int main() { string s1 Hello World 1234567; cout s1.capacity() endl; // 31 s1.erase(9); // 下标9处开始删除所有 cout s1.capacity() endl; // 31 s1.shrink_to_fit(); // 设置 cout s1.capacity() endl; // 15 return 0; }8.7 最大字符数max_size( )函数原型size_type max_size() const;示例string s1 Hello World 1234567; cout s1.max_size() endl;九. 查找与子串9.1 正向查找 : find( )函数原型* find 返回给定子字符串的首次出现位置 * size_type find( CharT ch, size_type pos 0 ) const; * size_type find( const CharT* s, size_type pos 0 ) const; * size_type find( const CharT* s, size_type pos, size_type count) const; * size_type find( const string str, size_type pos 0 ) const; * 参数pos: 起始查找位置 参数counts前count个字符作为被查找子串示例int main() { string s1 aabbcdeffggg; cout s1.find(b) endl; // 2 下标 cout s1.find(b, 5) endl; // 从下标5-d处向后找找不到 cout s1.find(bc) endl; // 3 下标 cout s1.find(bcccc, 0, 2) endl;// 3 从下标0-a向后找找bc string s2 bc; cout s1.find(s2) endl; // 3 下标 return 0; }9.2 反向查找 rfind( )函数原型* find 找到一个子字符串的最后一次出现位置 * size_type rfind( CharT ch, size_type pos 0 ) const; * size_type rfind( const CharT* s, size_type pos 0 ) const; * size_type rfind( const CharT* s, size_type pos, size_type count) const; * size_type rfind( const string str, size_type pos 0 ) const; * 参数pos: 起始查找位置 参数counts前count个字符作为被查找子串示例int main() { string s1 aabbcdeffbcgbg; cout s1.rfind(b) endl; // 11 倒数第二个 cout s1.rfind(b, 5) endl; // 3 从下标5-d处向前找 cout s1.rfind(bc) endl; // 9 倒数第五处 cout s1.rfind(bbcc, 5, 3) endl; // 2从下标5-d处向前找找bbc string s2 bc; cout s1.rfind(s2) endl; // 3 下标 return 0; }9.3 找子串中的字符(左)find_first_of( )子串中的任意一个字符在大串中第一次出现的位置。函数原型* string对象中第一个属于指定字符串中任何字符的元素 * size_type find_first_of( CharT ch, size_type pos 0 ) const; * size_type find_first_of( const CharT* s, size_type pos 0 ) const; * size_type find_first_of( const CharT* s, size_type pos, size_type count) const; * size_type find_first_of( const string str, size_type pos 0 ) const;示例int main() { string s1 aabbcdeffbcgbg; cout s1.find_first_of(b) endl; // 2 倒数第二个 cout s1.find_first_of(b, 5) endl; // 9 从下标5-d处向后找 cout s1.find_first_of(cba) endl; // 0 a出现在下标0的位置 cout s1.find_first_of(cefdf, 5, 3) endl; // 6从下标5-d处向后找cef string s2 bc; cout s1.find_first_of(s2) endl; // 2 下标 return 0; }9.4 找非子串中的字符(左)find_first_not_of( )大串中第一个不在子串中的字符。即该字符不跟子串中的任何一个都不一样。函数原型* 对象中第一个不属于指定字符串中任何字符的元素返回下标 * size_type find_first_not_of( CharT ch, size_type pos 0 ) const; * size_type find_first_not_of( const CharT* s, size_type pos 0 ) const; * size_type find_first_not_of( const CharT* s, pos, size_type count) const; * size_type find_first_not_of( const string str, size_type pos 0 ) const;示例int main() { string s1 abdef1234; cout s1.find_first_not_of(a) endl; // 1 第一个不一样b cout s1.find_first_not_of(a, 5) endl; // 5 从1处向后找1不一样 cout s1.find_first_not_of(cba) endl; // 2 d跟cba不一样 cout s1.find_first_not_of(cefdf, 3, 3) endl; // 5从e处向后找cef,1不一样 string s2 bc; cout s1.find_first_not_of(s2) endl; // 0 a跟bc不一样 return 0; }9.5 找子串中的字符(右)find_last_of( )函数原型* string对象中最后一个属于指定字符串中任何字符的元素 * size_type find_last_of( CharT ch, size_type pos 0 ) const; * size_type find_last_of( const CharT* s, size_type pos 0 ) const; * size_type find_last_of( const CharT* s, size_type pos, size_type coun; * size_type find_last_of( const string str, size_type pos 0 ) const;示例int main() { string s1 aabbcdeffbcgbg; cout s1.find_last_of(b) endl; // 12 倒数第二个b cout s1.find_last_of(b, 5) endl; // 3 从d处向左找b cout s1.find_last_of(ca) endl; // 10 从右向左c第一次出现 cout s1.find_last_of(cefdf, 5, 3) endl; // 4从d处向左找cef第一次出现c string s2 gc; cout s1.find_last_of(s2) endl; // 13 从右向左第一次出现g return 0; }9.6 找非子串中的字符(右)find_last_not_of( )函数原型* 对象中最后一个不属于指定字符串中任何字符的元素 * size_type find_last_not_of( CharT ch, size_type pos 0 ) const; * size_type find_last_not_of( const CharT* s, size_type pos 0 ) const; * size_type find_last_not_of( const CharT* s,size_type pos, size_type count) const; * size_type find_last_not_of( const string str, size_type pos 0 )示例int main() { // 从右向左找 string s1 abdef1234; cout s1.find_last_not_of(a) endl; // 8 第一个不一样4 cout s1.find_last_not_of(a, 5) endl; // 5 从1处向左找abdef11不一样 cout s1.find_last_not_of(cba) endl; // 8 4跟cba不一样 cout s1.find_last_not_of(cefdf, 3, 3) endl; // 2 从e处向左找cef,d不一样 string s2 bc; cout s1.find_last_not_of(s2) endl; // 8 4跟bc不一样 return 0; }9.7 提取子串substr( )函数原型* string substr( size_type pos 0, size_type count npos ) const;取出其中一段pos起始位置count是字符数。示例int main() { string s abcdefghijklmn; cout s.substr(3) endl; // defghijklmn cout s.substr(3, 5) endl; // defgh return 0; }十. string比较10.1 关系运算符、!、、、、按字典序比较。从左向右依次比较每一个字符。函数原型** 比较两个string对象 bool operator( const string lhs, const string rhs ); bool operator!( const string lhs, const string rhs ); bool operator( const string lhs, const string rhs ); bool operator( const string lhs, const string rhs ); bool operator( const string lhs, const string rhs ); bool operator( const string lhs, const string rhs ); ** 比较string对象与字符串 bool operator( const string lhs, const CharT* rhs ); bool operator( const CharT* lhs, const string rhs ); bool operator!( const string lhs, const CharT* rhs ); bool operator!( const CharT* lhs, const string rhs ); bool operator( const string lhs, const CharT* rhs ); bool operator( const CharT* lhs, const string rhs ); bool operator( const string lhs, const CharT* rhs ); bool operator( const CharT* lhs, const string rhs ); bool operator( const string lhs, const CharT* rhs ); bool operator( const CharT* lhs, const string rhs ); bool operator( const string lhs, const CharT* rhs ); bool operator( const CharT* lhs, const string rhs );示例int main() { string s1 abcdef; string s2 abcd; cout (s1 s2) endl; // 0 cout (s1 s2) endl; // 1 cout (s1 abcdef) endl; // 1 cout (efgh ! s2) endl; // 1 return 0; }10.2 比较函数compare( )提示返回负值小于、0等于、正值大于。函数原型int compare( const string str ) const; int compare( size_type pos1, size_type count1,const string str ) const; int compare( size_type pos1, size_type count1, const string str, size_type pos2, size_type count2 npos ) const; int compare( const CharT* s ) const; int compare( size_type pos1, size_type count1,t CharT* s ) const; int compare( size_type pos1, size_type count1,const CharT* s, size_type count2 ) const; template class StringViewLike int compare( const StringViewLike t ) const noexcept(); template class StringViewLike int compare( size_type pos1, size_type count1,const StringViewLike t ) const; template class StringViewLike int compare( size_type pos1, size_type count1, StringViewLike t, size_type pos2, size_type count2 npos) const;pos1与count1用来指定对象字符串的一段。pos2与count2用来指定参数被比较字符串的一段。示例int main() { string s1 abcdef; string s2 abcd; cout s1.compare(1, 2, s2) endl; // 1 s1bc s2(abcd) cout s1.compare(1, 2, s2, 1, 2) endl; // 0 s1bc s2(bc) string_view s3 s2; cout s1.compare(1, 2, s3) endl; // 1 s1bc s3(abcd) cout s1.compare(1, 2, s3, 1, 2) endl; // 0 s1bc s3(bc) return 0; }十一. 数值与字符串互转11.1 字符串 - 浮点型stof( ) / stod( ) / stold( )函数原型* string转换成float * float stof ( const string str, size_t* pos nullptr ); * string转换成double * double stod ( const string str, size_t* pos nullptr ); * string转换成long double * long double stold( const string str, size_t* pos nullptr ); * 参数2返回string中的数字字符的位数pos参数指定字符串开始转换的下标位置。示例int main() { string s 123.4567; size_t st 2; cout std::setprecision(7) stof(s, st) endl; //123.4567 cout st endl; // 8 共8个字符 return 0; }11.2 字符串 - 整型stoi( ) / stol( ) / stoll( )* stoi 转换成int * int stoi(const std::string str, std::size_t* pos nullptr, int base 10 ); * stol 转换成long * long stol(const std::string str, std::size_t* pos nullptr, int base 10 ); * stoll 转换成long long * long long stoll(const std::string str,std::size_t* pos nullptr, int base 10 ); * 参数2返回string中的数字字符的位数 * 参数3指示字符串的数字是几进制默认10进制注意返回结果是10进制的。示例int main() { string s 1234567; size_t st 0; cout stoi(s, st, 10) endl;//1234567是10进制 cout st endl; // 7 共7个字符 cout stoi(s, st, 8) endl; //1234567是8进制 cout st endl; // 7 共7个字符 return 0; }11.3 字符串 - 无符号整型stoul( ) / stoull( )* stoul 转换成unsigned long * unsigned long stoul(const std::string str, std::size_t* pos nullptr, int base 10 ); * stoull 转换成unsigned long long * unsigned long long stoull(const std::string str, std::size_t* pos nullptr, int base 10 ); * 参数2返回string中的数字字符的位数 * 参数3指示字符串的数字是几进制默认10进制示例int main() { string s 1234567; size_t st 0; cout stoull(s, st, 10) endl;//1234567是10进制 cout st endl; // 7 共7个字符 cout stoul(s, st, 8) endl; //1234567是8进制 cout st endl; // 7 共7个字符 return 0; }11.4 数值 - 字符串stringto_string( )函数原型* to_string 一个数值转换成string * std::string to_string( int value ); * std::string to_string( long value ); * std::string to_string( long long value ); * std::string to_string( unsigned value ); * std::string to_string( unsigned long value ); * std::string to_string( unsigned long long value ); * std::string to_string( float value ); * std::string to_string( double value ); * std::string to_string( long double value );示例int main() { string s1 to_string(12); cout s1 endl; string s2 to_string(12.45f); cout s2 endl; string s3 to_string(124ul); cout s3 endl; return 0; }十二. 输入输出运算符 / 函数原型ostream operator( ostream os, const string str ); istream operator( istream is, string str ); istream getline( istream input, string str, CharT delim ); istream getline( istream input, string str, CharT delim ); istream getline( istream input, string str ); istream getline( istream input, string str );示例#include iostream #include string using namespace std; int main() { string s; cout 输入一个单词: ; cin s; // 遇空格结束 cout 你输入了: s endl; cin.ignore(); // 忽略残留换行符 cout 输入一行文字: ; getline(cin, s); // 读取整行包括空格 cout 你输入了: s endl; return 0; }十三. 其它实用函数13.1 交换内容swap函数原型* 交换两个string对象 * void swap( string other );示例string a Hello, b World; a.swap(b); // 或 swap(a, b); cout a , b endl; // World, Hello13.2 拷贝出一段到数组中copy函数原型* 指定下标处拷贝一段装进一个指定的字符数组中 * size_type copy( CharT* dest, size_type count, size_type pos 0);示例int main() { string s abcdefghijklmn; char arr[10] {0}; s.copy(arr, 5, 1); // 从下标1处拷贝出5个字符 cout arr endl; // bcdef return 0; }13.3 C20 新增starts_with/ends_with/contains函数原型* starts_with 验证起始字符或字符串是否为参数指定的 * bool starts_with( CharT ch ) const; * bool starts_with( const CharT* s ) const; * ends_with 验证结尾字符或字符串是否为参数指定的 * bool ends_with( CharT ch ) const; * bool ends_with( const CharT* s ) const; * contains 验证结是否包含参数指定的字符或字符串 * bool contains( CharT ch ) const; * bool contains( const CharT* s ) const;示例string s Hello C20; if (s.starts_with(Hello)) cout starts with Hello endl; if (s.ends_with(20)) cout ends with 20 endl; if (s.contains(C)) cout contains C endl;

更多文章