博客
关于我
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/

你可能感兴趣的文章
Objective-C实现莱布尼兹级数求解π的近似值(附完整源码)
查看>>
Objective-C实现获取 Collatz 序列长度算法(附完整源码)
查看>>
Objective-C实现获取CPU温度(附完整源码)
查看>>
Objective-C实现获取GPU显卡信息(附完整源码)
查看>>
Objective-C实现获取HID设备列表 (附完整源码)
查看>>
Objective-C实现获取PE文件特征(附完整源码)
查看>>
Objective-C实现获取文件头的50个字符(附完整源码)
查看>>
Objective-C实现获取文件最后修改时间(附完整源码)
查看>>
Objective-C实现获取本机ip及mac地址(附完整源码)
查看>>
Objective-C实现获取本机系统版本(附完整源码)
查看>>
Objective-C实现视频流转换为图片(附完整源码)
查看>>
Objective-C实现视频除雾算法(附完整源码)
查看>>
Objective-C实现解密 Atbash 密码算法(附完整源码)
查看>>
Objective-C实现解密藏头诗(附完整源码)
查看>>
Objective-C实现解释器模式(附完整源码)
查看>>
Objective-C实现计算排列和组合的数量算法 (附完整源码)
查看>>
Objective-C实现读写蓝牙串口(附完整源码)
查看>>
Objective-C实现辗转相除法(附完整源码)
查看>>
Objective-C实现遗传算法(附完整源码)
查看>>
Objective-C实现醉汉随机行走问题(附完整源码)
查看>>