跳到主要内容
C++ STL string 类核心用法与接口详解 | 极客日志
C++
C++ STL string 类核心用法与接口详解 C++ STL string 类涵盖构造、容量管理、访问遍历及修改操作。通过示例展示了空串、C 风格字符串及拷贝构造等初始化方式;解析了 size、capacity、empty 等容量查询函数,区分了 reserve 预分配与 resize 调整长度的差异。内容还包含下标与迭代器访问、push_back 尾插、append 追加、find 查找及 substr 截取等常用接口,最后补充了运算符重载与非成员函数的输入输出处理,为实际开发提供完整参考。
清酒独酌 发布于 2026/3/22 更新于 2026/4/25 1 浏览前言
STL 中的 string 类是处理文本数据最常用的容器之一。在掌握迭代器、auto 关键字和范围 for 循环的基础上,深入理解 string 的常用接口能显著提升开发效率。本文将重点梳理 string 类的构造、容量管理、访问遍历及修改操作。
一、string 类对象的常见构造
1.1 空字符串构造函数
无参构造函数 string() 会创建一个长度为 0 的空字符串对象。
#include <iostream>
#include <string>
using namespace std;
int main () {
string s1;
cout << "s1: " << s1 << endl;
cout << "s1.size(): " << s1. size () << endl;
return 0 ;
}
1.2 C 字符串构造函数
可以通过 C 风格字符串(const char*)直接初始化 string 对象。
#include <iostream>
#include <string>
using namespace std;
int main () {
string s2 ("Hello world" ) ;
cout << "s2: " << s2 << endl;
return 0 ;
}
1.3 n 个字符构造函数
指定数量 n 和字符 c,构造包含 n 个 c 的字符串。
std;
{
;
cout << << s3 << endl;
;
}
#include <iostream>
#include <string>
using
namespace
int main ()
string s3 (5 , 'x' )
"s3: "
return
0
1.4 拷贝构造函数 支持通过另一个 string 对象进行拷贝初始化。
#include <iostream>
#include <string>
using namespace std;
int main () {
string original = "Hello C++" ;
string str (original) ;
cout << "str: " << str << endl;
return 0 ;
}
二、string 类的容量操作
2.1 size #include <iostream>
#include <string>
using namespace std;
int main () {
string s = "Hello World" ;
cout << "s.size(): " << s.size () << endl;
return 0 ;
}
2.2 capacity 返回当前为字符串分配的存储空间大小,通常大于或等于 size。
#include <iostream>
#include <string>
using namespace std;
int main () {
string s1 ("Hello World" ) ;
cout << "s1.capacity(): " << s1. capacity () << endl;
return 0 ;
}
2.3 empty #include <iostream>
#include <string>
using namespace std;
int main () {
string s1 = "hello world" ;
string s2;
cout << "s1.empty(): " << s1. empty () << endl;
cout << "s2.empty(): " << s2. empty () << endl;
return 0 ;
}
2.4 clear 清空字符串内容,使其变为空串,但可能保留原有容量。
#include <iostream>
#include <string>
using namespace std;
int main () {
string s = "Hello World" ;
s.clear ();
cout << "After clear, s: " << s << endl;
cout << "s.size(): " << s.size () << endl;
cout << "s.capacity(): " << s.capacity () << endl;
return 0 ;
}
2.5 reserve 请求将字符串容量调整为至少 n 个字符。这不会改变字符串长度,仅影响底层内存分配,常用于避免频繁扩容。
#include <iostream>
#include <string>
using namespace std;
int main () {
string str1;
string str2;
cout << "=== 不使用 reserve ===" << endl;
cout << "初始 str1 容量:" << str1. capacity () << ", 长度:" << str1.l ength() << endl;
str1 = "This is a demonstration of the string reserve function in C++." ;
cout << "赋值后 str1 容量:" << str1. capacity () << ", 长度:" << str1.l ength() << endl;
cout << "\n=== 使用 reserve ===" << endl;
cout << "初始 str2 容量:" << str2. capacity () << ", 长度:" << str2.l ength() << endl;
str2. reserve (100 );
cout << "reserve(100) 后,str2 容量:" << str2. capacity () << ", 长度:" << str2.l ength() << endl;
str2 = "This is a demonstration of the string reserve function in C++." ;
cout << "赋值后 str2 容量:" << str2. capacity () << ", 长度:" << str2.l ength() << endl;
return 0 ;
}
2.6 resize 调整字符串的实际长度。若新长度小于原长度则截断;若大于则填充(可指定填充字符)。
#include <iostream>
#include <string>
using namespace std;
int main () {
string s = "Hello C" ;
s.resize (9 , '+' );
cout << "s: " << s << endl;
cout << "s.size(): " << s.size () << endl;
s.resize (5 );
cout << "s: " << s << endl;
cout << "s.size(): " << s.size () << endl;
return 0 ;
}
三、string 类的访问及遍历操作
3.1 operator[] #include <iostream>
#include <string>
using namespace std;
int main () {
string s2 ("hello world" ) ;
cout << "下标访问元素:" ;
for (int i = 0 ; i < s2. size (); i++) {
cout << s2[i] << " " ;
}
s2[0 ] = 'H' ;
cout << "\n更改后下标位置 2 处的字符串:" ;
for (int i = 0 ; i < s2. size (); i++) {
cout << s2[i] << " " ;
}
return 0 ;
}
3.2 迭代器获取:begin 和 end #include <iostream>
#include <string>
using namespace std;
int main () {
string s2 ("hello world" ) ;
cout << "迭代器访问元素:" ;
string::iterator it = s2. begin ();
while (it != s2. end ()) {
cout << *it << " " ;
++it;
}
cout << "\n利用迭代器更改元素:" ;
it = s2. begin ();
while (it != s2. end ()) {
*it += 2 ;
cout << *it << " " ;
++it;
}
return 0 ;
}
3.3 范围 for 循环 #include <iostream>
#include <string>
using namespace std;
int main () {
string s2 ("hello world" ) ;
cout << "范围 for 循环访问元素:" ;
for (auto & ch : s2) {
cout << ch << " " ;
}
cout << "\n范围 for 循环更改元素:" ;
for (auto & ch : s2) {
ch += 2 ;
cout << ch << " " ;
}
return 0 ;
}
四、string 类的修改操作
4.1 push_back #include <iostream>
#include <string>
using namespace std;
int main () {
string s ("hello world" ) ;
cout << "尾插前的 s:" << s << endl;
s.push_back (' ' );
s.push_back ('s' );
cout << "push_back 尾插字符后的 s:" << s << endl;
return 0 ;
}
4.2 operator+= 和 append operator+= 更常用,支持追加字符、C 字符串或 string 对象;append 功能类似但略显繁琐。
#include <iostream>
#include <string>
using namespace std;
int main () {
string s ("hello world" ) ;
cout << "尾插前的 s:" << s << endl;
s += 'x' ;
cout << "尾插单个字符的 s:" << s << endl;
s += " Welcome" ;
cout << "尾插 C 类型字符串后的 s:" << s << endl;
string s2 (" Hello C++" ) ;
s += s2;
cout << "尾插字符串 s2 后的 s:" << s << endl;
return 0 ;
}
4.3 c_str 转换为 C 风格字符串指针,常用于与旧 API 交互。
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main () {
string s = "C++" ;
const char * p = s.c_str ();
cout << "C-string: " << p << endl;
cout << "Length via strlen: " << strlen (p) << endl;
return 0 ;
}
4.4 find 查找子串或字符首次出现的位置,未找到返回 string::npos。
#include <iostream>
#include <string>
using namespace std;
int main () {
string s ("text.cpp.zip" ) ;
size_t pos1 = s.find ('.' );
cout << "字符串 s 中 . 的位置为:" << pos1 << endl;
size_t pos2 = s.find ("cpp" );
cout << "字符串 s 中 cpp 的位置为:" << pos2 << endl;
size_t pos3 = s.find ("C++" );
if (pos3 == string::npos) {
cout << "Not find!" << endl;
}
return 0 ;
}
4.5 substr #include <iostream>
#include <string>
using namespace std;
int main () {
string str = "We think in generalities, but we live in details." ;
string str2 = str.substr (3 , 5 );
cout << "截取得到下标从 3 开始之后的五个字符:" << str2 << endl;
size_t pos = str.find ("live" );
string str3 = str.substr (pos);
cout << "截取 pos 位置之后的字符串:" << str3 << '\n' ;
return 0 ;
}
4.6 insert #include <iostream>
#include <string>
using namespace std;
int main () {
string s ("hello world" ) ;
cout << "尾插前的 s:" << s << endl;
s.insert (0 , " Byte " );
cout << "insert 在下标 0 处插入字符串后的 s:" << s << endl;
string s2 (" Welcome to C++" ) ;
size_t pos = s.size ();
s.insert (pos, s2);
cout << "insert 在下标 pos 处插入字符串后的 s:" << s << endl;
s.insert (0 , 5 , '6' );
cout << "insert 在下标 0 处插入 5 个字符后的 s:" << s << endl;
return 0 ;
}
4.7 erase #include <iostream>
#include <string>
using namespace std;
int main () {
string str ("This is an example sentence." ) ;
cout << str << '\n' ;
str.erase (10 , 8 );
cout << str << '\n' ;
return 0 ;
}
五、string 类的非成员函数
5.1 operator+ #include <iostream>
#include <string>
using namespace std;
int main () {
string firstlevel ("com" ) ;
string secondlevel ("cplusplus" ) ;
string scheme ("http://" ) ;
string hostname;
string url;
hostname = "www." + secondlevel + '.' + firstlevel;
url = scheme + hostname;
cout << url << '\n' ;
return 0 ;
}
5.2 operator>> 和 operator<< 标准输入输出流的重载,>> 遇空格停止,<< 直接输出。
#include <iostream>
#include <string>
using namespace std;
int main () {
string s;
cout << "Enter a word: " ;
cin >> s;
cout << "You entered: " << s << endl;
string name = "Alice" ;
cout << "Name: " << name << endl;
return 0 ;
}
5.3 getline 读取整行输入,包括空格,直到遇到换行符或指定分隔符。
#include <iostream>
#include <string>
using namespace std;
int main () {
string line;
cout << "Enter a line: " ;
getline (cin, line);
cout << "Line: " << line << endl;
getline (cin, line, ';' );
cout << "Until ';': " << line << endl;
return 0 ;
}
5.4 字符串大小比较 支持字典序比较(==, !=, <, >, <=, >=)。
#include <iostream>
#include <string>
using namespace std;
int main () {
string foo = "alpha" ;
string bar = "beta" ;
if (foo == bar) cout << "foo and bar are equal\n" ;
if (foo != bar) cout << "foo and bar are not equal\n" ;
if (foo < bar) cout << "foo is less than bar\n" ;
if (foo > bar) cout << "foo is greater than bar\n" ;
if (foo <= bar) cout << "foo is less than or equal to bar\n" ;
if (foo >= bar) cout << "foo is greater than or equal to bar\n" ;
return 0 ;
}
相关免费在线工具 Base64 字符串编码/解码 将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
Base64 文件转换器 将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
Markdown转HTML 将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online
HTML转Markdown 将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML转Markdown在线工具,online
JSON 压缩 通过删除不必要的空白来缩小和压缩JSON。 在线工具,JSON 压缩在线工具,online
JSON美化和格式化 将JSON字符串修饰为友好的可读格式。 在线工具,JSON美化和格式化在线工具,online