博客
关于我
C++STL详解(一)—— string类
阅读量:798 次
发布时间:2023-04-03

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

C++ STL string类详解

C++标准库中的string类是处理字符串操作的高效工具,支持多种字符串操作,包括构造、插入、拼接、删除、查找、比较、替换、交换等功能。以下从多个方面详细介绍string类的使用方法。

1. string的定义方式

string类支持多种构造方式以满足不同场景的需求,常用的构造函数包括:

  • string();

    构造一个空字符串。

  • string(const char* s);

    复制s所指的字符序列。

  • string(const char* s, size_t n);

    复制s所指字符序列的前n个字符。

  • string(size_t n, char c);

    生成nc字符的字符串。

  • string(const string& str);

    生成str的复制品。

  • string(const string& str, size_t pos, size_t len = npos);

    复制str中从pos开始并跨越len个字符的部分。

使用示例

string s1;                     // 构造空字符串string s2("hello string");     // 复制"hello string"string s3("hello string", 3);  // 复制"hello string"的前3个字符string s4(10, 's');            // 生成10个's'字符的字符串string s5(s2);                 // 生成s2的复制品string s6(s2, 0, 4);           // 复制s2中从字符位置0开始并跨越4个字符的部分

2. string的插入

1. 使用push_back进行尾插

push_back函数用于在字符串末尾插入一个字符:

#include 
#include
using namespace std;int main() { string s; s.push_back('C'); s.push_back('S'); s.push_back('D'); s.push_back('N'); cout << s << endl; // 输出"CSDN" return 0;}

2. 使用insert插入

insert函数用于在指定位置插入字符、字符串或字符数组:

#include 
#include
using namespace std;int main() { string s("C"); // insert(pos, str)在pos位置插入字符串str s.insert(1, "S"); // "CS" // insert(pos, string)在pos位置插入string对象 string t("D"); s.insert(2, t); // "CSD" // insert(pos, char)在pos位置插入字符char s.insert(s.end(), 'N'); // "CSDN" cout << s << endl; // 输出"CSDN" return 0;}

3. string的拼接

使用append函数完成字符串的拼接:

#include 
#include
using namespace std;int main() { string s1("I"); string s2(" like"); // append(string)完成两个string对象的拼接 s1.append(s2); // "I like" // append(str)完成string对象和字符串str的拼接 s1.append(" C++"); // "I like C++" // append(n, char)将n个字符char拼接到string对象后面 s1.append(3, '!'); // "I like C++!!!" cout << s1 << endl; // 输出"I like C++!!!" return 0;}

4. string的删除

1. 使用pop_back进行尾删

pop_back函数用于删除字符串末尾的字符:

#include 
#include
using namespace std;int main() { string s("C++"); s.pop_back(); // "C" s.pop_back(); // "C" cout << s << endl; // 输出"C" return 0;}

2. 使用erase删除

erase函数用于删除字符串中的指定部分:

#include 
#include
using namespace std;int main() { string s("I like C++!!!"); // erase(pos, n)删除pos位置开始的n个字符 s.erase(8, 5); // "I like C" // erase(pos)删除pos位置的字符 s.erase(s.end() - 1); // "I like" // erase(pos1, pos2)删除[pos1, pos2)上所有字符 s.erase(s.begin() + 1, s.end()); // "I" cout << s << endl; // 输出"I" return 0;}

5. string的查找

1. 使用find函数正向搜索

find函数用于查找字符串中第一个匹配项:

#include 
#include
using namespace std;int main() { string s1("http://www.cplusplus.com/reference/string/string/find/"); // find(string)正向搜索与string对象所匹配的第一个位置 string s2("www"); size_t pos1 = s1.find(s2); // 输出7 // find(str)正向搜索与字符串str所匹配的第一个位置 char str[] = "cplusplus.com"; size_t pos2 = s1.find(str); // 输出11 // find(char)正向搜索与字符char所匹配的第一个位置 size_t pos3 = s1.find(':'); // 输出4 cout << pos1 << endl << pos2 << endl << pos3 << endl; // 输出"7 11 4" return 0;}

2. 使用rfind函数反向搜索

rfind函数用于查找字符串中第一个反向匹配项:

#include 
#include
using namespace std;int main() { string s1("http://www.cplusplus.com/reference/string/string/find/"); // rfind(string)反向搜索与string对象所匹配的第一个位置 string s2("string"); size_t pos1 = s1.rfind(s2); // 输出42 // rfind(str)反向搜索与字符串str所匹配的第一个位置 char str[] = "reference"; size_t pos2 = s1.rfind(str); // 输出25 // rfind(char)反向搜索与字符char所匹配的第一个位置 size_t pos3 = s1.rfind('/'); // 输出53 cout << pos1 << endl << pos2 << endl << pos3 << endl; // 输出"42 25 53" return 0;}

6. string的比较

compare函数用于比较两个字符串:

#include 
#include
using namespace std;int main() { string s1("hello world"); string s2("hello CSDN"); // 比较"hello world"和"hello CSDN" cout << s1.compare(s2) << endl; // 输出1 // 比较"ell"和"hello CSDN" cout << s1.compare(1, 3, s2) << endl; // 输出-1 // 比较"hello"和"hello" cout << s1.compare(0, 4, s2, 0, 4) << endl; // 输出0 return 0;}

7. string的替换

replace函数用于替换字符串中的部分内容:

#include 
#include
using namespace std;int main() { string s("hello world"); // replace(pos, len, str)将pos位置开始的len个字符替换为字符串str s.replace(6, 4, "CSDN"); // "hello CSDNd" // replace(pos, len, n, char)将pos位置开始的len个字符替换为n个字符char s.replace(10, 1, 3, '!'); // "hello CSDN!!!" cout << s << endl; // 输出"hello CSDN!!!" return 0;}

8. string的交换

swap函数用于交换两个string对象的内容:

#include 
#include
using namespace std;int main() { string s1("hello"); string s2("CSDN"); // 使用string类的成员函数swap交换s1和s2 s1.swap(s2); cout << s1 << endl; // 输出"CSDN" cout << s2 << endl; // 输出"hello" // 使用非成员函数swap交换s1和s2 swap(s1, s2); cout << s1 << endl; // 输出"hello" cout << s2 << endl; // 输出"CSDN" return 0;}

9. string的大小和容量

1. 使用size或length获取当前字符个数

#include 
#include
using namespace std;int main() { string s("CSDN"); cout << s.size() << endl; // 输出4 cout << s.length() << endl; // 输出4 return 0;}

2. 使用max_size获取最大可容纳字符数

#include 
#include
using namespace std;int main() { string s("CSDN"); cout << s.max_size() << endl; // 输出4294967294 return 0;}

3. 使用capacity获取当前存储空间大小

#include 
#include
using namespace std;int main() { string s("CSDN"); cout << s.capacity() << endl; // 输出15 return 0;}

4. 使用resize改变字符个数

#include 
#include
using namespace std;int main() { string s1("CSDN"); // resize(n) n大于当前size时,扩大到n,默认填充'\0' s1.resize(20); cout << s1 << endl; // 输出"CSDN" cout << s1.size() << endl; // 输出20 cout << s1.capacity() << endl; // 输出31 string s2("CSDN"); // resize(n, char) n大于当前size时,扩大到n,填充指定字符 s2.resize(20, 'x'); cout << s2 << endl; // 输出"CSDNxxxxxxxxxxxxxxxx" cout << s2.size() << endl; // 输出20 cout << s2.capacity() << endl; // 输出31 // resize(n) n小于当前size时,缩小到n s3.resize(2); cout << s3 << endl; // 输出"CS" cout << s3.size() << endl; // 输出2 cout << s3.capacity() << endl; // 输出15 return 0;}

5. 使用reserve预留存储空间

#include 
#include
using namespace std;int main() { string s("CSDN"); // reserve(n) n大于当前capacity时,扩大到n或更大 s.reserve(20); cout << s << endl; // 输出"CSDN" cout << s.size() << endl; // 输出4 cout << s.capacity() << endl; // 输出31 // reserve(n) n小于当前capacity时,无效果 s.reserve(2); cout << s << endl; // 输出"CSDN" cout << s.size() << endl; // 输出4 cout << s.capacity() << endl; // 输出31 return 0;}

6. 使用clear删除内容

#include 
#include
using namespace std;int main() { string s("CSDN"); s.clear(); cout << s << endl; // 输出空字符串 return 0;}

7. 使用empty判断是否为空

#include 
#include
using namespace std;int main() { string s("CSDN"); cout << s.empty() << endl; // 输出0 s.clear(); cout << s.empty() << endl; // 输出1 return 0;}

10. string中元素的访问

1. 使用[]+下标访问元素

#include 
#include
using namespace std;int main() { string s("CSDN"); // []+下标访问元素 for (size_t i = 0; i < s.size(); i++) { cout << s[i]; } cout << endl; // 输出"CSDN" // 修改元素 for (size_t i = 0; i < s.size(); i++) { s[i] = 'x'; } cout << s << endl; // 输出"xxxx" return 0;}

2. 使用at访问元素

#include 
#include
using namespace std;int main() { string s("CSDN"); // at(pos)访问pos位置的元素 for (size_t i = 0; i < s.size(); i++) { cout << s.at(i); } cout << endl; // 输出"CSDN" // 修改元素 for (size_t i = 0; i < s.size(); i++) { s.at(i) = 'x'; } cout << s << endl; // 输出"xxxx" return 0;}

3. 使用范围for访问元素

#include 
#include
using namespace std;int main() { string s("CSDN"); // 使用范围for访问元素 for (auto e : s) { cout << e; } cout << endl; // 输出"CSDN" // 使用范围for修改元素,e必须是引用类型 for (auto& e : s) { e = 'x'; } cout << s << endl; // 输出"xxxx" return 0;}

4. 使用迭代器访问元素

#include 
#include
using namespace std;int main() { string s("CSDN"); // 使用迭代器访问元素 string::iterator it1 = s.begin(); while (it1 != s.end()) { cout << *it1; it1++; } cout << endl; // 输出"CSDN" // 修改元素 string::iterator it2 = s.begin(); while (it2 != s.end()) { *it2 += 1; it2++; } cout << s << endl; // 输出"DTEO" return 0;}

11. string中的运算符使用

1. operator=

string类对=运算符进行了重载,支持多种赋值方式:

#include 
#include
using namespace std;int main() { string s1; string s2("CSDN"); // string类赋值 s1 = s2; // "CSDN" // 字符串赋值 s1 = "hello"; // 字符赋值 s1 = 'x'; cout << s1 << endl; // 输出"x" return 0;}

2. operator+=

string类对+=运算符进行了重载,支持多种复合赋值方式:

#include 
#include
using namespace std;int main() { string s1; string s2("hello"); // string类复合赋值 s1 += s2; // "hello" // 字符串复合赋值 s1 += " CSDN"; // "hello CSDN" // 字符复合赋值 s1 += '!'; // "hello CSDN!" cout << s1 << endl; // 输出"hello CSDN!" return 0;}

3. operator+

string类对+运算符进行了重载,支持多种字符串操作:

#include 
#include
using namespace std;int main() { string s; string s1("super"); string s2("man"); char str[] = "woman"; char ch = '!'; // string类 + string类 s = s1 + s2; // "superman" // string类 + 字符串 s = s1 + str; // "superwoman" // 字符串 + string类 s = str + s1; // "womansuper" // string类 + 字符 s = s1 + ch; // "super!" // 字符 + string类 s = ch + s1; // "!super" cout << s << endl; // 输出"!super" return 0;}

4. relational operators

string类对多个关系运算符进行了重载,支持字符串间的比较:

#include 
#include
using namespace std;int main() { string s1("abcd"); string s2("abde"); // 比较s1和s2 cout << (s1 > s2) << endl; // 0 cout << (s1 < s2) << endl; // 1 cout << (s1 == s2) << endl; // 0 return 0;}

12. string与迭代器相关的函数

1. 正向迭代器

#include 
#include
using namespace std;int main() { string s("hello string"); string::iterator it = s.begin(); while (it != s.end()) { cout << *it; it++; } cout << endl; // 输出"hello string" return 0;}

2. 反向迭代器

#include 
#include
using namespace std;int main() { string s("hello string"); string::reverse_iterator rit = s.rbegin(); while (rit != s.rend()) { cout << *rit; rit++; } cout << endl; // 输出"gnirts olleh" return 0;}

13. string与字符串之间的转换

1. 将字符串转换为string

#include 
#include
using namespace std;int main() { // 方式一 string s1("hello world"); // 方式二 char str[] = "hello world"; string s2(str); cout << s1 << endl; // 输出"hello world" cout << s2 << endl; // 输出"hello world" return 0;}

2. 使用c_str或data转换为字符串

#include 
#include
using namespace std;int main() { string s("hello world "); const char* str1 = s.data(); const char* str2 = s.c_str(); cout << str1 << endl; // 输出"hello world " cout << str2 << endl; // 输出"hello world " return 0;}

14. string中的子字符串提取

1. 使用substr提取子字符串

#include 
#include
using namespace std;int main() { string s1("abcdef"); string s2; // 提取pos位置开始的len个字符 s2 = s1.substr(2, 4); // "cdef" cout << s2 << endl; // 输出"cdef" return 0;}

2. 使用copy复制子字符串到数组

#include 
#include
using namespace std;int main() { string s("abcdef"); char str[20]; size_t length = s.copy(str, 4, 2); // 复制从位置2开始的4个字符 str[length] = '\0'; cout << str << endl; // 输出"dcef" return 0;}

15. string中的getline函数

getline函数用于读取包含空格的字符串:

#include 
#include
using namespace std;int main() { string s; getline(cin, s); // 读取包含空格的字符串 cout << s << endl; // 输出"hello CSDN" return 0;}

2. 使用特定分隔符的getline

#include 
#include
using namespace std;int main() { string s; getline(cin, s, 'D'); // 使用'D'作为分隔符 cout << s << endl; // 输出"hello CS" return 0;}

以上内容系统地介绍了C++ STL中的string类,涵盖了从基础到高级操作,希望能为开发者提供全面且实用的参考。

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

你可能感兴趣的文章
Oracle触发器
查看>>
oracle触发器
查看>>
Oracle计划将ZGC项目提交给OpenJDK
查看>>
oracle账号共享
查看>>
Oracle闪回技术(Flashback)
查看>>
oracle零碎要点---ip地址问题,服务问题,系统默认密码问题
查看>>
oracle零碎要点---oracle em的web访问地址忘了
查看>>
Oracle零碎要点---多表联合查询,收集数据库基本资料
查看>>
Oracle静默安装
查看>>
【Bert101】变压器模型背后的复杂数学【02/4】
查看>>
Oracle面试题:Oracle中truncate和delete的区别
查看>>
ThreadLocal线程内部存储类
查看>>
thinkphp 常用SQL执行语句总结
查看>>
Oracle:ORA-00911: 无效字符
查看>>
Text-to-Image with Diffusion models的巅峰之作:深入解读 DALL·E 2
查看>>
Tensorflow.python.framework.errors_impl.ResourceExhaustedError:无法分配内存[操作:AddV2]
查看>>
TCP基本入门-简单认识一下什么是TCP
查看>>
tableviewcell 中使用autolayout自适应高度
查看>>
Symbolic Aggregate approXimation(SAX,符号聚合近似)介绍-ChatGPT4o作答
查看>>
Orcale表被锁
查看>>