【C++】string

目录

1.STL简介

1.1 什么是STL

1.2 STL六大组件

2.string概念

3.string常用接口说明

3.1 string构造

3.2 string容量操作

3.3 string访问及遍历操作

3.4 stirng修改操作

3.5 string非成员函数


1.简介

1.1 什么是STL

STL(standard template libaray- 标准模板库 ) C++ 标准库的重要组成部分 ,不仅是一个可复用的组件库,而且 是一个包罗数据结构与算法的软件框架

1.2 STL六大组件

2.string概念

1. 字符串是表示字符序列的类
2. 标准的字符串类提供了对此类对象的支持,其接口类似于标准字符容器的接口,但添加了专门用于操作
单字节字符字符串的设计特性。
3. string类是使用char(即作为它的字符类型,使用它的默认char_traits和分配器类型(关于模板的更多信息,请参阅basic_string)。
4. string类是basic_string模板类的一个实例,它使用char来实例化basic_string模板类,并用char_traits和allocator作为basic_string的默认参数(根于更多的模板信息请参考basic_string)。
5. 注意,这个类独立于所使用的编码来处理字节:如果用来处理多字节或变长字符(如UTF-8)的序列,这个类的所有成员(如长度或大小)以及它的迭代器,将仍然按照字节(而不是实际编码的字符)来操作。
总结:
1. string是表示字符串的字符串类
2. 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作。
3. string在底层实际是:basic_string模板类的别名,typedef basic_string<char, char_traits, allocator> string;
4. 不能操作多字节或者变长字符的序列。
使用string类时,必须包含#include头文件以及using namespace std;

3.string常用接口说明

3.1 string构造

1.直接创建:string();

2.创建并插入字符串:string (const char* s);

3.创建与其他字符串相同的新字符串:string (const string& str);

4.创建含n个相同的字符的字符串:string (size_t n, char c);

5.创建一个含指定字符串前n个字符的新字符串:string (const char* s, size_t n);

6.创建含一个字符串指定位置开始前n个字符的新字符串:string (const string& str, size_t pos, size_t len = npos);(最后一个参数不写默认把该位置后面全部字符复制)

3.2 string容量操作

1.size&&length:返回字符串有效字符长度

2.capacity:返回空间总大小

3.empty:检测字符串释放为空串,是返回true,否则返回false

4.clear:清空有效字符

	string s("12345");
	cout << s << endl;
	cout << s.size() << endl;
	cout << s.length() << endl;
	cout << s.capacity() << endl;
	cout << s.empty() << endl<<endl;
	s.clear();
	cout << s << endl;
	cout << s.size() << endl;
	cout << s.length() << endl;
	cout << s.capacity() << endl;
	cout << s.empty() << endl<<endl;

输出结果:

5.reserve:为字符串预留空间

	string s("12345");
	cout << s << endl;
	cout << s.size() << endl;
	cout << s.capacity() << endl<<endl;
	//1.reserve参数<=string的底层空间总大小(即capacity)时,reserver不会改变容量大小。
	s.reserve(14);
	cout << s << endl;
	cout << s.size() << endl;
	cout << s.capacity() << endl << endl;
	//2.reserve参数>string的空间大小,reserve会进行相应地扩容
	s.reserve(20);
	cout << s << endl;
	cout << s.size() << endl;
	cout << s.capacity() << endl << endl;

输出结果:

6.resize:将有效字符的个数该成n个,多出的空间用字符c填充

//resize 重新设定string大小(size)
	//这里分三种情况:1.resize值<=size;2.resize值>size&&resize值<=capacity;3.resize值>capacity
	//1.resize参数<=size
	string s1("hhcyyds");
	cout << s1 << endl;
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;
	s1.resize(3);
	cout << s1 << endl;
	cout << s1.size() << endl;
	cout << s1.capacity() << endl << endl;
	//2.resize参数>size&&resize参数<=capacity
	string s2("hhcyyds");
	cout << s2 << endl;
	cout << s2.size() << endl;
	cout << s2.capacity() << endl;
	s2.resize(14,'6');
	cout << s2 << endl;
	cout << s2.size() << endl;
	cout << s2.capacity() << endl<<endl;
	//3.resize参数>capacity
	string s3("hhcyyds");
	cout << s3 << endl;
	cout << s3.size() << endl;
	cout << s3.capacity() << endl;
	s3.resize(20, '6');
	cout << s3 << endl;
	cout << s3.size() << endl;
	cout << s3.capacity() << endl << endl;

输出结果:

注意:
1. size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一
致,一般情况下基本都是用size()。
2. clear()只是将string中有效字符清空,不改变底层空间大小。
3. resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字
符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的
元素空间。注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大
小,如果是将元素个数减少,底层空间总大小不变。
4. reserve(size_t res_arg=0):为string预留空间,不改变有效元素个数,当reserve的参数小于
string的底层空间总大小时,reserver不会改变容量大小。

3.3 string访问及遍历操作

1.operator[]重载遍历:返回pos位置的字符,const string类对象调用

2.迭代器遍历:begin(rbegin)获取一个字符的迭代器 + end(rend)获取最后一个字符下一个位置的迭代器

3.范围for遍历:C++11支持更简洁的范围for的新遍历方式

string s1("12345");
	cout << s1 << endl;
	//1.下标遍历
	for (int i = 0; i < s1.size(); i++)
	{
		s1[i] += 1;
	}
	cout << s1 << endl;
	//2.范围for遍历
	for (auto& t: s1)
	{
		t++;
	}
	cout << s1 << endl;
	//3.迭代器遍历
	string::iterator t = s1.begin();
	while (t != s1.end())
	{
		*t += 1;
		t++;
	}
	cout << s1 << endl;
	string::reverse_iterator rt = s1.rbegin();
	while (rt != s1.rend())
	{
		cout << *rt;
		rt++;
	}

输出结果:

3.4 stirng修改操作

1.push_back :在字符串后尾插字符c
2.append: 在字符串后追加一个字符串
3.operator+= (重点) : 在字符串后追加字符串str
string s("hhc");
	cout << s << endl;
	s.append("yyds");
	cout << s << endl;
	s.push_back('1');
	cout << s << endl;
	s += "666";
	cout << s << endl;

输出结果:

4.c_str(重点) : 返回C格式字符串
    string s("hhcyyds1");
	cout << s.c_str() << endl;
输出结果:
5.find + npos(重点): 从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置
6.rfind: 从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置
	string s("hhchhc");
	cout << s.find('c') << endl;
	cout << s.rfind('c') << endl;
	cout << s.find('c',3) << endl;
	cout << s.rfind('c',3) << endl;
输出结果:
7.substr: 在str中从pos位置开始,截取n个字符,然后将其返回
    string s("hhcyyds1");
	cout << s.substr(3, 4) << endl;
	cout << s.substr(3) << endl;

输出结果:

注意:
1. 在string尾部追加字符时,s.push_back(c) / s.append(1, c) / s += 'c'三种的实现方式差不多,一般情况下string类的+=操作用的比较多,+=操作不仅可以连接单个字符,还可以连接字符串。
2. 对string操作时,如果能够大概预估到放多少字符,可以先通过reserve把空间预留好

3.5 string非成员函数

1.operator+ : 尽量少用,因为传值返回,导致深拷贝效率低
2.operator>> 重点):输入运算符重载
3.operator<< (重点):输出运算符重载
4.getline (重点):获取一行字符串
5.relational operators (重点):大小比较
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_hhc_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值