跳到主要内容 C++ String 详解 | 极客日志
C++ 算法
C++ String 详解 本文详细介绍了 C++ 标准库中 string 类的定义、初始化方式、常用迭代器、运算符重载及核心成员函数(如 size、find、insert、erase 等),并涵盖了全局函数(getline、stoi 等)及范围 for 循环的使用。通过代码示例展示了字符串处理、查找替换、类型转换等功能,适合 C++ 初学者参考。
星星泡饭 发布于 2026/3/29 更新于 2026/4/13 2 浏览前言
本文详细介绍了 C++ 中 string 类的使用方法,主要包括:1. 多种初始化方式(空字符串、C 风格字符串、重复字符等);2. 迭代器类型及遍历方法;3. 常用运算符重载(赋值、连接、比较等);4. 核心成员函数(查找、插入、删除、替换等);5. 全局函数(类型转换、输入输出等);6. 基于范围的 for 循环遍历。文章通过具体代码示例演示了 string 类的各种操作,包括字符串处理、查找替换、类型转换等功能,涵盖了 string 类的主要应用场景。
一、定义和初始化 string
在 C++ 中,string 是标准库中提供的字符串类,定义在 <string> 头文件中,位于 std 命名空间内。
#
std;
include
<string>
using
namespace
string s8 (s2. begin(), s2. begin() + 5 ) ;
string s6 ("Hello, World!" , 5 ) ;
string s4 (s2) ;
string s5 = s2;
string s3 = "Hello, World!" ;
string s2 ("Hello, World!" ) ;
二、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++)
*i3 = toupper (*i3);
cout << s << endl;
return 0 ;
}
说明: toupper 函数把小写字母转为大写字母,该函数是 C 语言函数,由于 C++ 兼容 C 也可以直接使用,但需要引用对应的头文件 cctype。
三、string 常用运算符 string 类重载了多种运算符,方便字符串操作:
运算符 含义 s1=s2 把 s2 的值赋值给 s1 s1+s2 返回 s1 和 s2 连接后的结果 s1==s2 判断是否相等,区分大小写 s1!=s2 判断是否不相等 <,<=,>,>= 判断大小关系,利用字典序进行比较,区分大小写 s1+=s2 在 s1 的后面连接 s2 s1[i] 返回 s1 第 i 个字符的引用,从 0 开始 cout<<s 输出 s 到输出流 cout cin>>s 从 cin 输入流读取字符串到 s
int main () {
string s1 ("abcde" ) ;
string s2;
string s3;
s2 = s1;
s3 = s1 + s2;
cout << "s1:" << s1 << endl;
cout << "s2:" << s2 << endl;
cout << "s3:" << s3 << endl;
if (s1 == s2)
cout << "s1==s2" << endl;
if (s1 != s2)
cout << "s1!=s2" << endl;
if (s1 < s3)
cout << "s1<s3" << endl;
else if (s1 > s3)
cout << "s1>s3" << endl;
else
cout << "s1==s3" << endl;
s1 += s2;
cout << "s1:" << s1 << endl;
cout << "s2:" << s2 << endl;
s1[0 ] = 'x' ;
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 ();
cout << s.length ();
2. find 成员函数
功能 :在字符串中查找子串或字符的第一次出现位置
参数 :
str:要查找的子串
pos:开始查找的位置(默认为 0)
返回值 :找到的位置,若未找到则返回 string::npos
string s = "Hello, World!" ;
size_t pos = s.find ("World" );
if (pos != string::npos) {
cout << "Found at position: " << pos;
}
3. rfind 成员函数
功能 :在字符串中查找子串或字符的最后一次出现位置
参数 :
str:要查找的子串
pos:开始查找的位置(默认为 string::npos,表示从字符串末尾开始)
返回值 :找到的位置,若未找到则返回 string::npos
string s = "Hello, World!" ;
size_t pos = s.rfind ("o" );
if (pos != string::npos) {
cout << "Found at position: " << pos;
}
4. find_first_of 成员函数
功能 :在字符串中查找第一个属于指定字符集的字符
参数 :
str:包含要查找的字符集的字符串
pos:开始查找的位置(默认为 0)
返回值 :找到的位置,若未找到则返回 string::npos
string s = "Hello, World!" ;
size_t pos = s.find_first_of ("aeiou" );
if (pos != string::npos) {
cout << "First vowel at position: " << pos;
}
5. find_first_not_of 成员函数
功能 :在字符串中查找第一个不属于指定字符集的字符
参数 :
str:包含要排除的字符集的字符串
pos:开始查找的位置(默认为 0)
返回值 :找到的位置,若未找到则返回 string::npos
string s = "Hello, World!" ;
size_t pos = s.find_first_not_of ("Helo" );
if (pos != string::npos) {
cout << "First non-'Helo' character at position: " << pos;
}
6. find_last_of 成员函数
功能 :在字符串中查找最后一个属于指定字符集的字符
参数 :
str:包含要查找的字符集的字符串
pos:开始查找的位置(默认为 string::npos,表示从字符串末尾开始)
返回值 :找到的位置,若未找到则返回 string::npos
string s = "Hello, World!" ;
size_t pos = s.find_last_of ("aeiou" );
if (pos != string::npos) {
cout << "Last vowel at position: " << pos;
}
7. find_last_not_of 成员函数
功能 :在字符串中查找最后一个不属于指定字符集的字符
参数 :
str:包含要排除的字符集的字符串
pos:开始查找的位置(默认为 string::npos,表示从字符串末尾开始)
返回值 :找到的位置,若未找到则返回 string::npos
string s = "Hello, World!" ;
size_t pos = s.find_last_not_of ("!" );
if (pos != string::npos) {
cout << "Last non-'!' character at position: " << pos;
}
8. empty 成员函数
功能 :判断字符串是否为空
参数 :无
返回值 :如果字符串为空返回 true,否则返回 false
string s1;
string s2 = "Hello" ;
cout << s1. empty ();
cout << s2. empty ();
9. insert 成员函数
功能 :在指定位置插入 字符串或字符
参数 :
返回值 :插入后的字符串引用
string s = "Hello World" ;
s.insert (5 , "," );
10. erase 成员函数
功能 :删除字符串中的部分字符
参数 :
pos:开始删除的位置(默认为 0)
len:要删除的字符数(默认为 string::npos,表示删除到字符串末尾)
返回值 :删除后的字符串引用
string s = "Hello, World!" ;
s.erase (5 , 2 );
11. push_back 成员函数
功能 :在字符串末尾添加一个字符
参数 :
返回值 :无
string s = "Hello" ;
s.push_back ('!' );
12. pop_back 成员函数
功能 :删除字符串末尾的一个字符
参数 :无
返回值 :无
string s = "Hello!" ;
s.pop_back ();
13. replace 成员函数
功能 :替换字符串中的部分字符
参数 :
pos:开始替换的位置
len:要替换的字符数
str:替换的字符串
返回值 :替换后的字符串引用
string s = "Hello, World!" ;
s.replace (7 , 5 , "C++" );
14. substr 成员函数
功能 :返回字符串的子串
参数 :
pos:子串的起始位置
len:子串的长度(默认为 string::npos,表示到字符串末尾)
返回值 :子串
string s = "Hello, World!" ;
string sub = s.substr (7 , 5 );
15. swap 成员函数 string s1 = "Hello" ;
string s2 = "World" ;
s1. swap (s2);
16. c_str 成员函数
功能 :返回一个指向 C 风格字符串的指针(以 null 结尾)
参数 :无
返回值 :指向 C 风格字符串的指针
string s = "Hello" ;
const char * cstr = s.c_str ();
cout << cstr;
17. at 成员函数
功能 :访问指定位置的字符 (进行边界检查)
参数 :
返回值 :指定位置的字符的引用
异常 :如果位置超出范围,抛出 out_of_range 异常
string s = "Hello" ;
char c = s.at (0 );
try {
c = s.at (10 );
} catch (const out_of_range& e) {
cout << "Exception: " << e.what ();
}
五、用于 string 的全局函数 下面的函数都不是 string 的成员函数,而是用于 string 比较常用的全局函数。
全局函数 作用 getline 从输入流读取一行字符 stod 将字符序列转换为 double stoi 将字符序列转换为 int stoull 将字符序列转换为 unsigned long long to_string 把数字转成对应的字符串
1. getline 函数
功能 :从输入流中读取一行字符串,直到遇到换行符
参数 :
返回值 :输入流对象的引用
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. stoi 函数
功能 :将字符串转换为 int 类型
参数 :
str:要转换的字符串
idx:可选参数,用于存储转换后第一个未使用字符的位置
base:可选参数,转换的基数(默认为 10)
返回值 :转换后的 int 值
string s = "123" ;
int i = stoi (s);
cout << i;
4. stoull 函数
功能 :将字符串转换为 unsigned long long 类型
参数 :
str:要转换的字符串
idx:可选参数,用于存储转换后第一个未使用字符的位置
base:可选参数,转换的基数(默认为 10)
返回值 :转换后的 unsigned long long 值
string s = "123456789012345" ;
unsigned long long ull = stoull (s);
cout << ull;
5. 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;
六、基于范围 for 处理每个字符 C++11 引入了基于范围的 for 循环 ,可以更简洁地遍历字符串中的每个字符:
string s ("hello" ) ;
for (auto x : s)
cout << x << " " ;
for (auto & c : s)
c = toupper (c);
cout << s << endl;
如果只是读取字符,使用 auto c
如果需要修改字符,使用 auto& c
基于范围的 for 循环会自动遍历字符串中的每个字符,不需要手动管理索引或迭代器
微信扫一扫,关注极客日志 微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
相关免费在线工具 加密/解密文本 使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
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