博客
关于我
C++标准模版库STL——String
阅读量:538 次
发布时间:2019-03-07

本文共 4840 字,大约阅读时间需要 16 分钟。

标准库类型 String

字符串类型(String)是 C++ 标准库中用来表示可变长字符序列的组件。要使用 string 类型,需在代码中包含 string 头文件(

),而其定义位于 std 命名空间中。由于字符串操作是一种常见需求,了解其使用方法对开发 workflow 有重要意义。

string 类常见用法详解

通过下标访问

C++ 代码示例:

#include 
#include
using namespace std;int main() { string str = "abcd"; for (int i = 0; i < str.length(); i++) { cout << static_cast
(str[i]) << " "; } return 0;}

输出示例

a b c d

注意:对于字符串操作,可以通过 cincout 读取和输出整个字符串。若要逐个字符处理,才需要使用下标。

通过 c_str() 方法可以将 string 类型转换为 C 语言中的字符串数组进行输出。

c_str() 方法

C++ 代码示例:

#include 
#include
using namespace std;int main() { string str = "abcd"; cout << str.c_str() << endl; return 0;}

输出示例

abcd

通过迭代器访问

C++ 代码示例:

#include 
#include
using namespace std;int main() { string str = "abcd"; for (string::iterator it = str.begin(); it != str.end(); ++it) { cout << static_cast
(*it) << " "; } return 0;}

注意:在 STL 中,只有 string 和 vector 类支持直接对迭代器进行加减某个数字操作,如 str.begin() + 3

string 类常用函数实例解析

operator +=

两个 string 类型可以直接拼接。

C++ 代码示例:

#include 
#include
using namespace std;int main() { string str1 = "abc", str2 = "xyz", str3; str3 = str1 + str2; str1 += str2; cout << str1 << endl; cout << str3 << endl; return 0;}

输出示例

abcxyzabcxyz

比较操作符(compare operator)

可以使用 ==!=<<=>>= 等比较 string 类型的大小,比较规则基于字典序。

C++ 代码示例:

#include 
#include
using namespace std;int main() { string str1 = "abc", str2 = "xyz"; if (str1 == str2) { cout << "两者相等" << endl; } else { cout << "不相等" << endl; } return 0;}

length() 和 size()

两者返回字符串的长度,可以使用任之一:

C++ 代码示例:

#include 
#include
using namespace std;int main() { string str1 = "abc", str2 = "xyz", str3; str3 = str1 + str2; str1 += str2; cout << str1 << endl; cout << str3 << endl; cout << str1.length() << endl; cout << str1.size() << endl; return 0;}

输出示例

abcxyzabcxyz77

string 类高级操作插件

insert() 方法

可以在指定位置插入子字符串。

C++ 代码示例:

#include 
#include
using namespace std;int main() { string str = "abcxyz", str2 = "opq"; str.insert(3, str2); cout << str << endl; return 0;}

注意str2 或者迭代器范围可以作为插入内容的来源。

例如,使用迭代器:

string str = "abcxyz", str2 = "opq";str.insert(str.begin() + 3, str2.begin(), str2.end());

输出示例

abcopqxyz

erase() 方法

用于删除单个或多个字符。

C++ 代码示例(删除单个字符):

#include 
#include
using namespace std;int main() { string str = "abcxyz", str2 = "opq"; str.erase(str.begin() + 4); cout << str << endl; return 0;}

输出示例

abxyz

另一个示例(删除特定区间):

#include 
#include
using namespace std;int main() { string str = "abcdefg"; str.erase(str.begin() + 2, str.end() - 1); cout << str << endl; return 0;}

输出示例

abcdef

erase(pos,length)

传入起始位置和要删除的字符个数。

C++ 代码示例:

#include 
#include
using namespace std;int main() { string str = "abcdefg"; str.erase(3, 2); cout << str << endl; return 0;}

输出示例

abcd

clear() 方法

用于清空字符串中的所有数据。

C++ 代码示例:

#include 
#include
using namespace std;int main() { string str = "abcdefg"; str.clear(); cout << str.length() << endl; return 0;}

输出示例

0

substr() 方法

用于提取子字符串。

C++ 代码示例:

#include 
#include
using namespace std;int main() { string str = "Welcome to Changsha!"; cout << str.substr(0, 5) << endl; cout << str.substr(14, 4) << endl; return 0;}

输出示例

Welcomesha!

string::npos 常数

表示无效子字符串的位置,值为 -1。

C++ 代码示例:

#include 
#include
using namespace std;int main() { if (string::npos == -1) { cout << "-1 is true." << endl; } return 0;}

输出示例

-1是真实的。

find() 方法

用于寻找子字符串的位置,返回 string::npos 若不存在。

C++ 代码示例:

#include 
#include
using namespace std;int main() { string str = "Thank you for your smile."; string str2 = "you"; string str3 = "me"; if (str.find(str2) != string::npos) { cout << str.find(str2) << endl; } if (str.find(str2, 7) != string::npos) { cout << str.find(str2, 7) << endl; } if (str.find(str3) != string::npos) { cout << str.find(str3) << endl; } else { cout << "I know there is no position for me." << endl; } return 0;}

输出示例

715-1是真实的。

在调用 find 方法前,建议先测试子字符串是否存在于主字符串中,以避免莫须有情况。

replace() 方法

用于替换字符串中特定范围内的子串。

C++ 代码示例:

#include 
#include
using namespace std;int main() { string str = "Maybe you will turn around."; string str2 = "will not"; string str3 = "surely"; cout << str.replace(10, 4, str2) << endl; cout << str.replace(str.begin(), str.begin() + 5, str3) << endl; return 0;}

输出示例

Maybe you will not.you.rely

最终总结

通过以上实例可以发现,string 类 提供了丰富的操作方法,使得字符串处理变得高效和直观。在开发中,合理使用这些方法可以提高代码的可维护性和可读性。随着对 C++ 标准库的深入理解,可以进一步发掘更多高级功能,提升编程能力。

转载地址:http://bvynz.baihongyu.com/

你可能感兴趣的文章
Plotly:如何从单条迹线制作堆积条形图?
查看>>
Plotly:如何以 Root 样式绘制直方图,仅显示直方图的轮廓?
查看>>
Plotly:如何使用 Plotly Express 组合散点图和线图?
查看>>
Plotly:如何使用 plotly.graph_objects 和 plotly.express 定义图形中的颜色?
查看>>
Plotly:如何使用 Python 对绘图对象条形图进行颜色编码?
查看>>
Plotly:如何使用 updatemenus 更新一个特定的跟踪?
查看>>
Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?
查看>>
Plotly:如何向烛台图添加交易量
查看>>
Plotly:如何在 plotly express 中找到趋势线的系数?
查看>>
Plotly:如何在桑基图中设置节点位置?
查看>>
pm2 start命令中的json格式详解
查看>>
pm2启动报错
查看>>
pm2通过配置文件部署nodejs代码到服务器
查看>>
PML调用PDMS内核命令研究
查看>>
PMM安装-第一篇
查看>>
PMP知识要点(第九章)
查看>>
PNETLab 镜像包官方下载太慢?不急,最新版本PNET_4.2.10分享!
查看>>
POCO库中文编程参考指南(4)Poco::Net::IPAddress
查看>>
Quartz基本使用(二)
查看>>
POC项目安装与使用指南
查看>>