当前位置: 首页 > news >正文

string类【C++】

标准库中的string(std::string)

我们通过阅读文档 猛戳此处来进行详细学习
通过文档发现,string的底层其实也是模板
在这里插入图片描述

string类所用的头文件 #include<string>

在这里插入图片描述

1. string 的构造

发现C++98标准中有七个构造函数

在这里插入图片描述

在这里插入图片描述
当主要还要参考
在这里插入图片描述
例:

	string s0;		//会去调用默认构造,空字符串string s1("hello string!");	//c_stringstring s2(s1);	//拷贝构造string s3(s1,6,3);	//下标是从0开始//构造n个字符string s6(6,'*');//operator+ 和 operator=string str1, str2,str3;str1 = "hello "; //c_stringstr2 = "string!";str3 = str1 + str2;cout << str3 << endl;

2. string 类的遍历

2.1 下标+[ ]

//使用下标+[]进行遍历string str1 = "hello string!";//遍历 字符串for (size_t i = 0; i < str1.size(); i++)	//s1.size()可以计算有多少个字符{cout << str1[i] << " ";	//采用下标+[]进行遍历的方式}cout << endl;	//换行

在这里插入图片描述

//当然string类中也有operator<<,operator<<的重载cout<<str1;

也可以通过下标+[ ] 的形式修改值

	//修改pos位置的字符for (size_t i = 0; i < str1.size(); i++){str1[i]++;cout << str1[i] << " ";}

注意这里的str1[i]++;相当于对下方代码返回的进行++

class string 
{
public:char& operator[](size_t pos) {return _str[pos];}private:char* _str;size_t _size;size_t _capacity;
};

这里的operator[ ] 有两种形式,读写 和 只读
在这里插入图片描述

	const string s2("11222");//s2[0]++; //不能修改

2.2 string 中的迭代器(Iterators)

在这里插入图片描述

(1) 迭代器遍历

	//迭代器 iterator //迭代器行为像指针一样的类型对象,定义区间左闭右开【)string s1 = "my string`s iterator";string::iterator it = s1.begin();	//s1.begin()记录第一个有效数据的位置while (it != s1.end())	//s1.end() 记录最后一个有效数据的下一个位置{//遍历cout << *it << " ";++it;}

在这里插入图片描述

(2) 使用迭代器修改内容

	while (it1 != s1.end()){//修改内容*it1 -= 3;++it1;}//再次遍历it1 = s1.begin();	//记得从begin开始while (it1 != s1.end())	//s1.end() 记录最后一个有效数据的下一个位置{//遍历cout << *it1 << " ";++it1;}cout << endl;

(3) 反向迭代器遍历
当想要去逆序遍历,就可以去用反向迭代器

	//反向迭代器string str1("hello world");string::reverse_iterator rit = str1.rbegin();while (rit != str1.rend()){cout << *rit << " ";rit++;} cout << endl;

在这里插入图片描述
注意下方的 const迭代器
在这里插入图片描述
小结一下:

一般分为四种迭代器:

  1. 正向迭代器 iterator
  2. 反向迭代器 reverse_iterator
  3. const 正向迭代器 const_iterator
  4. const 反向迭代器 const_reverse_iterator

2.3 范围for遍历

//范围for遍历string s1 = "hello world!";for (auto e : s1)cout << e << " ";

3. string 中的Capacity

在这里插入图片描述

3.1. size() 和 length()

计算字符串的大小一般情况下使用size(),当然length()也可以,都是计算字符串的实际长度。

//string::size
#include<iostream>
#include<string>
using namespace std;
int main()
{string str("this string");cout<<str.size()<<endl;	//结果输出 11cout<<str.length()<<endl;	//也是可以计算字符串的实际长度return 0;
}

上述都可以计算字符串的长度,更建议使用size。

3.2 capacity()

string的capacity是返回 当前为字符串分配的存储空间的大小,用字节表示。即,当前最多可以容纳多少个字符。

//capacity
string str1("hello world");
//注意不包含'\0'占有的空间
cout << str1.capacity()<< endl;
str1.clear();//清空数据,不清空间

3.3 max_size()

string中的max_size()是计算字符串可以到达的最大长度。

#include<iostream>
#include<string>
using namespace std;int main() 
{string str("hello");cout << str.size() << endl;//5cout << str.length() << endl;//5cout << str.capacity() << endl;cout << str.max_size() << endl;	//返回字符串可以达到的最大长度return 0;
}

在vs2022下的x86下的运行结果:
在这里插入图片描述

3.4 reserve()

用于主动管理内存分配的成员函数,它允许你预先为字符串分配足够的存储空间(即调整 capacity),避免后续操作中频繁的内存重新分配。

语法:void reserve (size_t n = 0);

  • 当n <= capacity()的时候,没有效果(不会去缩小当前的capacity)
  • 当n > capacity()的时候,会重新分配内存,让capacity() >= n
	string str("hello");cout << str.size() << endl;cout << str.capacity() << endl;str.reserve(16);cout << str.capacity() << endl;
//运行结果:
5
15
31
//注意不同编译环境下capacity()的大小会有所不同

3.5 clear()

string的clear 只是消除字符串的内容,将字符串变成空字符串(字符串长度是0),但是改变字符串的空间。

	string str("hello");cout << str.size() << endl;//5cout << str.capacity() << endl;//15str.clear();cout << str.size() << endl;	// 0cout << str.capacity() << endl; // 15 ,不改变其空间的大小//运行结果:
5
15
0
15

3.6 empty()

std::string::empty 是用来返回字符串是否为空。

	string str("hello");str.clear();cout << str.size() << endl;	// 0if (str.empty()){cout << "this str is empty" << endl;	//当前字符串长度为0}else {cout << "this str is non-empty" << endl;}//运行结果:
0
this str is empty

3.7 关于缩容shrink_to_fit() [C++11]

#include<iostream>
#include<string>using namespace std;int main() 
{string s1("hello 111111111111111111111111111111111111111111");cout << s1 << endl;cout << s1.capacity() << endl;cout << s1.size() << endl;s1.clear();	//先清空数据//缩容s1.shrink_to_fit();	//shrink_to_fit()是C++11标准中的cout << s1.capacity() << endl;cout << s1.size() << endl;return 0;
}

在这里插入图片描述

3.8 关于扩容 resize()

resize一般用于调整字符串的大小的。

语法:
void resize (size_t n);
void resize (size_t n, char c);


注意:

  • 当 n 小于当前字符串的长度时,则当前字符串将缩短为其前 n 个字符,并删除第 n 个字符以外的字符。
  • 当 n 大于当前字符串的长度时,则通过在末尾插入所需数量的字符来扩展当前内容,以达到 n 的大小。如果有指定的字符c,就使用字符c来填充,否则用’\0’来填充。
	//查看扩容机制string s;size_t sz = s.capacity();	//先拿到首次的容量cout << "init capacity: " << sz << endl;cout << "making s grow: \n";for (int i = 0; i < 100;++i){s.push_back('a');if (sz != s.capacity()) {sz = s.capacity();//自动扩容cout << "now capacity is: " << sz << endl;}}cout << "capacity is changed end \n";

基于vs2022版的编译器
在这里插入图片描述

在这里插入图片描述

	string s;//reserve 保留,相当与进行扩容s.reserve(100);size_t sz = s.capacity();	//先拿到首次的容量cout << "init capacity: " << sz << endl;

在这里插入图片描述
注意 当 reserve 设置的值比 capacity 大的时候会起作用

resize()的三种常见的情况:

情况1:当 n < size,会删除字符

	string s2("hello world!");cout << s2 << endl;cout << "size is :" << s2.size() << endl;cout << "capacity is :" << s2.capacity() << endl;cout << "----------------------------\n";//resize(n) 可以改变字符串的长度//当 n < size,会删除字符cout << "resize : \n";s2.resize(5);	//只会保留5个字符cout << s2 << endl;cout << "size is :" << s2.size() << endl;cout << "capacity is :" << s2.capacity() << endl;

在这里插入图片描述
情况2:当 size<n<capacity,会插入’\0’

	string s2("hello world!");cout << s2 << endl;cout << "size is :" << s2.size() << endl;cout << "capacity is :" << s2.capacity() << endl;cout << "----------------------------\n";//当 size<n<capacity,会插入'\0'cout << "resize : \n";s2.resize(14);	////改变长度为14cout << s2 << endl;cout << "size is :" << s2.size() << endl;cout << "capacity is :" << s2.capacity() << endl;

在这里插入图片描述

情况3:当 n>capacity,会进行扩容+插入

	string s2("hello world!");cout << s2 << endl;cout << "size is :" << s2.size() << endl;cout << "capacity is :" << s2.capacity() << endl;cout << "----------------------------\n";//当 n>capacity,会进行扩容+插入cout << "resize : \n";s2.resize(20);	//改变长度为20cout << s2 << endl;cout << "size is :" << s2.size() << endl;cout << "capacity is :" << s2.capacity() << endl;

在这里插入图片描述

4. sting中单个字符访问

在这里插入图片描述

4.1 operator[ ] 和 at 的区别

关于at

	string s1("hello string!");cout << s1[4] << endl;	// ocout << s1.at(4) << endl; // o//区别:在于越界检查不同try {s1[15];}catch (const exception& e) {cout << e.what() << endl;}

[ ] 越界是断言报错

在这里插入图片描述

	string s1("hello string!");cout << s1[4] << endl;cout << s1.at(4) << endl;////区别:在于越界检查不同//try //{//	s1[15];//}//catch (const exception& e) //{//	cout << e.what() << endl;//}//at 越界会抛异常try{s1.at(15);	//会抛异常}catch (const exception& e){cout << e.what() << endl;}

at越界会抛异常
在这里插入图片描述

4.2 back()和front()的其他用法

back和front的其他用法不仅仅可以获取单个字符,还可以修改字符。

	string s1("-ello String-");s1.back() = '!';s1.front() = 'H';cout << s1 << endl;//运行结果:
Hello String!

string中的back()和front()是C++11中的。

5. string中的 Modifiers

在这里插入图片描述

5.1 operator+= 、append() 和 push_back()

这三个函数共同作用:都可以在字符串的末尾上追加内容

5.1.1 operator+=

在这里插入图片描述

	//std::string::operator+=使用string family("zhang");	string name("feng");family += " san ";	// c-stringfamily += name;		// string	family += '!';		// character 单个字符串cout << family;	// zhang san feng!
5.1.2 append()

在这里插入图片描述

	//std::string::append()使用string str;string str1("hello");string str2 = "---world***";str.append(str1);	//stringstr.append(str2,3,5); //substring,如果 str2 太短或 sublen 为 string::npos,会直接到str2的末尾str.append("!!");	// c-stringstr.append("%%33%%",2);	//bufferstr.append(3,'6');	//fillstr.append(str2.begin()+8,str2.end());	//iteratorcout << str << endl;//打印结果:helloworld!!%%666***
5.1.3 push_back()

std::string::push_back() 只能追加单个字符。

	//std::string::push_back()string str = "hello";str.push_back('!');	//仅仅可以追加单个字符cout << str << endl;

5.2 assign()

为字符串分配一个新值,替换其当前内容。

在这里插入图片描述
发现assign()的用法与append()用法类似

	//std::string::assign()使用string str("hello");string str1("world");str.assign(str1);	//stringcout << str << endl; str.assign(str1,0,-1); //substringcout << str << endl; str.assign("HE");	//c-stringcout << str << endl; str.assign("HELLO",4);	//buffer cout << str << endl;str.assign(6,'6');	//fill cout << str << endl;str.assign(str1.begin(),str1.end());	//iteratorcout << str << endl;//打印结果:
world
world
HE
HELL
666666
world

5.3 insert()

std::string::insert() 成员函数用于在字符串的任意位置插入字符或子串。
在这里插入图片描述

	//std::string::insert()的使用string str("123");string str1("abc");//1 在pos位置插入str1str.insert(3,str1);	cout << str << endl;	//123abc//2 在pos位置插入部分字符串str.insert(6,str1,1,2);	cout << str << endl;	//123abcbc//3 在pos位置插入c-stringstr1.insert(3,"---");	cout << str1 << endl;	//abc---//4 在pos位置插入字符串s的前n个str1.insert(0, "111456",3);	 cout << str1 << endl;	//111abc---//5 在pos位置插入n个字符cstr1.insert(1,2,'2');	cout << str1 << endl;	//12211abc---//6 使用iterator控制插入位置 插入当个字符cstring::iterator its;its = str1.insert(str1.end(),'!');cout << str1 << endl;	//12211abc---!//7 在 str3下标处插入str4的前三个字符string str3 = "hello";string str4 = "12345";str3.insert(str3.begin()+2,str4.begin(),str4.begin()+3);//区间左闭右开cout << str3 << endl;	//he123llo//像insert erase replace 谨慎使用
//因为频繁使用效率比较低

5.4 erase()

erase()只是清出字符串数据,并不会清理空间。
在这里插入图片描述

	//std::string::erasestring str("hello-- world!");cout << str.size() << endl;		//字符长度14cout << str.capacity() << endl;	//当前空间15cout << str << endl;	//hello-- world!// 1 string& erase (size_t pos = 0, size_t len = npos);//删除指定位置的多个字符str.erase(5,2);	//清空pos位置开始的两个字符cout << str << endl;	//hello world!// 2 iterator erase (iterator p);//可以删除指定位置的单个字符str.erase(str.begin()+5);cout << str << endl;	//helloworld!// 3 iterator erase (iterator first, iterator last);//删除指定范围的多个字符str.erase(str.begin()+1,str.end()-3);cout << str << endl;	//hld!// 4 当没有给参数的时候,默认清空整个字符串str.erase();	cout << str << endl;cout << str.capacity() << endl;	//15cout << str.size() << endl;		//字符长度为0

5.5 replace() 和 swap()

replace()是替换字符串的一部分。
swap()是交互字符串。

replace()的一些简单用法:

	//std::string::replace()string str = "hello------world!";string str1 = "this";//1 替换部分字符串//将str的下标6开始的4个字符替换为"this"str.replace(6,4,str1);cout << str << endl;	//hello-this-world!//2 替换整个字符串str.replace(0,str.size(),"666");cout << str << endl;	//666//3 借助迭代器string st = "Hello, World!";string replacement = "C++";st.replace(st.begin() + 7, st.begin() + 12, replacement.begin(), replacement.end()); // 将 "World" 替换为 "C++"cout << st << endl;	//Hello, C++!

swap()的简单用法:

	//swap()string str("test swap!");string str1("TEST SWAP!");str.swap(str1);cout << str << endl;	//TEST SWAP!

5.6 pop_back()[C++11]

删除字符串的最后一个字符,从而有效地将其长度减少 1。

	//std::string::pop_back()string str("hello C++!!");str.pop_back();	//pop_back()是C++11标准中的cout << str << endl;//hello C++!

string中常用的还有 find、rfind、getline等函数。

string最常用的就是遍历和修改,上述介绍了部分的string的使用。

http://www.lryc.cn/news/590444.html

相关文章:

  • 面试问题:
  • BASE64编码通俗介绍
  • Towards Low Light Enhancement with RAW Images 论文阅读
  • 北京-4年功能测试2年空窗-报培训班学测开-第五十二天
  • linux服务器stress-ng的使用
  • WAMP允许远程访问
  • 30 天自制 C++ 服务器--Day3
  • 用python实现自动化布尔盲注
  • RHCSA(软链接与硬链接)
  • 高性能架构模式——高性能缓存架构
  • sqli-labs靶场通关笔记:第23关 注释符过滤
  • 二、CV_AlexNet
  • 81、面向服务开发方法
  • 关于SaaS业务模式及其系统架构构建的详细解析
  • 横向移动(下)
  • IPD-流程设计-TE角色说明书参考模板
  • 多维傅里叶变换性质与计算
  • CSS3动画基本使用——页面一打开盒子就从左边走向右边
  • 【尝试】本地部署openai-whisper,通过 http请求识别
  • C++-linux系统编程 11.常见问题与答案
  • 创建SprngBoot项目的四种方式
  • 降本增效利器:汽车制造中EtherCAT转PROFIBUS DP网关应用探析
  • 快速开发汽车充电桩的屏幕驱动与语音提示方案
  • 使用 SeaTunnel 建立从 MySQL 到 Databend 的数据同步管道
  • Mysql系列--1、库的相关操作
  • 在 IntelliJ IDEA 中添加框架支持的解决方案(没有出现Add Framework Support)
  • AI学习笔记三十一:YOLOv8 C++编译测试(OpenVINO)
  • 使用Telegraf从工业物联网设备收集数据的完整指南
  • Beautiful Soup(BS4)
  • ABP VNext + EF Core 二级缓存:提升查询性能