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

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

通过 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/

你可能感兴趣的文章
python3读取redis返回数据带有'b'的问题
查看>>
Python3连接MySQL
查看>>
Python关于isinstance函数的解析 | 附Demo(全)
查看>>
Python3:setup.py:完成所有任务的PIP安装(Build_ext+Install)
查看>>
python4.0 gil_Python 并发编程系列--1、浅谈GIL历史及对多线程的影响
查看>>
python60行绘图程序_天底下最简单的QT画图板,就一个类,60行代码
查看>>
Python: AES加密与解密
查看>>
Python: Range() 最大尺寸;动态的还是静态的?
查看>>
Python: Windows下用 multiprocessing 的深坑
查看>>
python:Could not find platform independent libraries <prefix>解决方法
查看>>
Python:datetime tzinfo 时区名称文档
查看>>
Python:deepcopy(list) vs new_list = old_list[:]
查看>>
Python:Google API - 从消息中获取 mimeTypes
查看>>
Python:Google API - 从消息中获取 mimeTypes
查看>>
Python:subprocess.call,stdout to file,stderr to file,在屏幕上实时显示stderr
查看>>
Python:TypeError:无法连接“str“和“int“对象
查看>>
Python:XPath 在 ElementTree 中不可用
查看>>
Python:从 FTP 服务器下载文件
查看>>
python:从列表中创建元组列表
查看>>
Python:从嵌套列表中删除重复项
查看>>