前言
💬 欢迎讨论:如果你在学习过程中有任何问题或想法,欢迎在评论区留言,我们一起交流学习。你的支持是我继续创作的动力! 👍 点赞、收藏与分享:觉得这篇文章对你有帮助吗?别忘了点赞、收藏并分享给更多的小伙伴哦!你们的支持是我不断进步的动力! 🚀 分享给更多人:如果你觉得这篇文章对你有帮助,欢迎分享给更多对C++感兴趣的朋友,让我们一起进步!
C++ string 类从入门到精通:
C++ 标准库中的 std::string 类是一个非常强大的工具,用于处理和操作字符串。它属于 <string> 头文件,并提供了一套丰富的功能和方法。以下是 std::string 类的一些主要特性和常用操作:
字符串是表示字符序列的类 1. 标准的字符串类提供了对此类对象的支持,其接口类似于标准字符容器的接口,但添加了专门用于操作单字节字符字符串的设计特性。 2. string类是使用char(即作为它的字符类型,使用它的默认char_traits和分配器类型(关于模板的更多信 息,请参阅basic_string)。 3. string类是basic_string模板类的一个实例,它使用char来实例化basic_string模板类,并用char_traits 和allocator(配置器)作为basic_string的默认参数(根于更多的模板信息请参考basic_string)。 注意,这个类独立于所使用的编码来处理字节:如果用来处理多字节或变长字符(如UTF-8)的序列,这个 类的所有成员(如长度或大小)以及它的迭代器,将仍然按照字节(而不是实际编码的字符)来操作。 总结: string是表示字符串的字符串类 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作。 string在底层实际是:basic_string模板类的别名,typedef basic_string<char, char_traits, allocator> string; 不能操作多字节或者变长字符的序列。 在使用string类时,必须包含#iostream头文件以及using namespace std;
string 类支持多种构造方式,以下是常见的构造函数:

下面简单演示上述多个函数如何使用和了解其功能,代码如下:
#include<iostream>
using namespace std;
int main()
{
string s2("hello world");
string s1;//构造一个空的字符串
string s3("C++");//使用字符串C++构造s3对象
string s4(10, 'c');//构造包含10个'c'的s4对象
string s5(s2);//使用s2拷贝构造s5
cout <<"s1:"<< s1 << endl;
cout <<"s3:"<< s3 << endl;
cout <<"s4:"<< s4 << endl;
cout <<"s5:"<< s5 << endl;
return 0;
}输出结果:
s1: s3:C++ s4:cccccccccc s5:hello world

#include<iostream>
using namespace std;
int main()
{
string s2("hello world");
cout << s2 << endl;
cout << s2.size() << endl;
cout << s2.length() << endl;
cout << s2.capacity() << endl;
s2.resize(5);
cout << s2 << endl;
//cout << s2.reserve() << endl;
s2.clear();
if (s2.empty())
cout << "s2为空字符串" << endl;
else {
cout << "s2不为空字符串" << endl;
}
s2.resize(100);
cout << s2 << endl;
return 0;
}hello world 11 11 15 hello s2为空字符串
补充:resize使用示例:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "Hello";
cout << "Size: " << s.size() << endl; // 返回字符串长度
cout << "Capacity: " << s.capacity() << endl; // 返回容量
s.resize(10, 'X'); // 将长度改为10,多出部分用'X'填充
cout << "Resized: " << s << endl;
s.clear(); // 清空字符串
cout << "Is empty: " << s.empty() << endl; // 检查是否为空
return 0;
}输出结果:
Size: 5 Capacity: 15 Resized: HelloXXXXX Is empty: 1
注意:
1. resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不 同的是当字符个数增多时:resize(n)用'\0'来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的元素空间。注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变。 2. reserve(size_t res_arg=0):为string预留空间,不改变有效元素个数,当reserve的参数小于string的底层空间总大小时,reserve不会改变容量大小。 3. size和capacity都不包括’\0’。
常见字符串遍历方法接口(interface):

#include<iostream>
#include<string>
using namespace std;
int main()
{
string s2("hello world");
s2[0] = 'x';
cout << s2 << endl;
cout<<s2.at(1) << endl;//返回指定位置的对象
string::iterator it = s2.begin();
//迭代器遍历
while (it != s2.end())
{
cout << *it;
++it;
}
cout << endl;
//范围for,auto自动推导类型
for (auto it = s2.begin(); it != s2.end(); it++)
cout << *it;
cout << endl;
return 0;
}输出结果:
xello world e xello world xello world
常见查找接口如下:

C++中string::npos的概念,它是size_t的最大值,表示字符串结束。
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
int main()
{
string s1("hello world hello bit!");
//find的使用
//在字符串中查找子字符串或字符,返回其首次出现的位置,找不到则返回 string::npos
size_t ret = s1.find('e');
if (ret == string::npos)
printf("未找到!\n");
else
{
cout << "ret:"<<ret << endl;
}
size_t ret2 = s1.find('o');
if (ret2 == string::npos)
printf("未找到!\n");
else
{
cout << "ret2:" << ret2 << endl;
}
//rfind的使用
//反向查找字符串,返回最后一次出现子串或字符的位置
size_t ret3 = s1.rfind('o');
if (ret3 == string::npos)
printf("未找到!\n");
else
{
cout << "ret3:" << ret3 << endl;
}
//find_first_of()
//查找指定字符集中的任意一个字符,返回第一次出现的索引
size_t ret4=s1.find_first_of("b");
if (ret4 == string::npos)
printf("未找到!\n");
else
{
cout << "ret4:" << ret4 << endl;
}
size_t ret5 = s1.find_first_of("bit");
if (ret5 == string::npos)
printf("未找到!\n");
else
{
cout << "ret5:" << ret5 << endl;
}
size_t ret6 = s1.find_last_of('t');
if (ret6 == string::npos)
printf("未找到!\n");
else
{
cout << "ret6:" << ret6 << endl;
}
size_t ret7 = s1.find_last_of("bi");
if (ret7 == string::npos)
printf("未找到!\n");
else
{
cout << "ret7:" << ret7 << endl;
}
return 0;
}输出结果:
ret:1 ret2:4 ret3:16 ret4:18 ret5:18 ret6:20 ret7:19

这几个接口比较简单,日期类实现过,这里就不再重复了.


#include<iostream>
using namespace std;
int main()
{
//我们将wzy换成C++
string s1("hello wzy hello java!");
cout << s1 << endl;
s1.replace(6, 3, "C++");//将string s1开始的第六个字符的后面三个字符换成C++
cout << s1 << endl;
return 0;
}输出结果:
hello wzy hello java! hello C++ hello java!

#include<iostream>
using namespace std;
int main()
{
string s1("hello wzy hello java!");
string s2=s1.substr(10, 11);//
cout << s2 << endl;
return 0;
}输出结果:
hello wzy hello java! hello java!

补充: 由于C++是很注重效率的编程语言,上诉两个接口函数插入数据前均需要移动原有数据,效率低下,不建议使用。
#include<iostream>
using namespace std;
int main()
{
string s1("hello wzy hello java!");
cout << "orign staus:"<<s1 << endl;
s1.insert(6, "n");
cout << "After insert:"<<s1 << endl;
s1.erase(6,1);
cout << "After erase:" << s1 << endl;
s1.erase(5);
cout << "After erase:" << s1 << endl;
return 0;
}输出结果:
orign staus:hello wzy hello java! After insert:hello nwzy hello java! After erase:hello wzy hello java! After erase:hello

#include<iostream>
#include<string>
using namespace std;
int main()
{
int num = 20;
string s1 = to_string(num);//将数值转换为字符串
cout << s1 << endl;
string s="123456";
int sNum=stoi(s);//将字符串转换为整数
cout << sNum << endl;
string s3("123");
float s4 = stof(s3);//将字符串转换为浮点数
cout << s4 << endl;
return 0;
}输出结果:
20 123456 123

最后
相信通过这篇文章你对C++STL->string的使用高级部分的有了初步的了解。如果此篇文章对你学习C++有帮助,期待你的三连,你的支持就是我创作的动力!!!
下一篇文章再会.