C++之string类

今天我们就来说说C++中的string类吧,再说string类之前,就先说说它怎么来的吧。

STL

1.什么是STL
解答:STL(standard template libaray-标准模板库):是C++标准库的重要组成部分,不仅是一个可复用的组件库,而且 是一个包罗数据结构与算法的软件框架。
2.STL的六大组件
(1)容器:常见的数据结构
(2)算法:与具体的数据结构相关的算法+通用算法
(3)迭代器:让算法对数据结构透明化
(4)适配器:stack/queue/priority_queue(优先级—队列)
(5)仿函数:(函数对象)可以向函数一样使用对象
(6)空间配置器:负责申请释放空间及空间管理
对于string类来说,它就是STL里的一个容器。

string类

1 、标准库中的string类:
(1)string是表示字符串的字符串类
(2)该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作。
(3)string在底层实际是:basic_string模板类的别名:
typedef basic_string<char, char_traits, allocator> string;
(4) 不能操作多字节或者变长字符的序列。
(5) 在使用string类时,必须包含#include头文件以及using namespace std
2、string类的常用接口(重点)
(1)string类中常见的构造函数

函数名称 功能说明
string() 构造空的string类对象,即空字符
string(const char* s) 用C-string来构造string类对象
string(size_t n,char c) string类对象包含n个字符c
string(const string&s) 拷贝构造函数
void test1()
{
	string s1;                 //构造空的string类对象s1
	string s2("hello world"); //用C格式字符串构造string类对象s2
	string s3(s2);           //拷贝构造s3
}

(2)string类对象的容量操作

函数名称 功能说明
size 返回字符串有效字符长度
length 返回字符串有效字符长度
capacity 返回空间总大小
empty 检测字符串是否为空串,是返回true,否则返回false
clear 清空有效字符
reserve 改变字符串空间容量
resize 将有效字符的个数改为n个,多出来的空间用字符c填充
void test2()
{
	string s("hello world");
	cout << s.size() << endl;
	cout << s.length() << endl;
	cout << s.capacity() << endl;

	//将s中的字符串清空,注意清空时只是将size清0,不改变底层空间的大小 
	s.clear();
	cout << s.size() << endl;
	cout << s.capacity() << endl;
}

运行结果:
在这里插入图片描述
注意:(1)size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,一般情况下基本都用size();
(2)clear()只是将string中的有效字符清空,不改变底层空间大小;

void test2()
{
	string s("hello");
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;
	
	//将s中的字符增加到10个,多出来的位置用'!'进行填充
	s.resize(10, '!');
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;

	//将s中的字符增加到20个,多处的位置用‘c’进行填充
	s.resize(20, 'c');
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;

	//将s中的字符缩小到5个
	s.resize(5);
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;
}

运行结果:

从上图中我们可以看出 resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的 元素空间。注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大 小,如果是将元素个数减少,底层空间总大小不变。

void test3()
{
	string s;
	//在VS编译器下原有的容量
	cout << s.size() << endl;
	cout << s.capacity() << endl;

	//将容量扩大到10
	s.reserve(10);
	cout << s.size() << endl;
	cout << s.capacity() << endl;

	//将容量扩大到30
	s.reserve(30);
	cout << s.size() << endl;
	cout << s.capacity() << endl;

	//将容量缩小到20
	s.reserve(20);
	cout << s.size() << endl;
	cout << s.capacity() << endl;

	//将容量缩小到10
	s.reserve(10);
	cout << s.size() << endl;
	cout << s.capacity() << endl;
}

运行结果:
在这里插入图片描述
从上图中我们可以看出. reserve(size_t res_arg=0):为string预留空间,**不改变有效元素个数,**当reserve的参数小于 string的底层空间总大小时,reserver不会改变容量大小。
(3)string类对象的访问及遍历操作

函数名称 功能说明
operator[] 下标访问符重载,返回pos位置的字符,const string类对象的调用
begin+end begin获取一个字符的迭代器+end获取最后一个字符下一个位置的迭代器
rbegin+rend begin获取一个字符的迭代器+end获取最后一个字符下一个位置的迭代器
范围for C++11支持更简洁的范围for的新遍历方式
void test5()
{
	//三种遍历方式
	string s("Hello wrold");
	//1.for+operator[]
	for (size_t i = 0; i < s.size(); ++i)
	{
		cout << s[i] ;
	}

	//2.范围for循环
	for (auto ch : s)
	{
		cout << ch << endl;
	}

	//3.迭代器
	//方法一
	string::iterator it = s.begin();
	while (it != s.end())
	{
		cout << *it << endl;
		++it;
	}
//方法二
	string::reverse_iterator rit = s.rbegin();
	while (rit != s.rend())
	{
		cout << *rit << endl;
	}
}

(4)string类对象的修改操作

函数名称 功能说明
push back 在字符串后尾插字符c
append 在字符串后追加一个字符串
operator+= 在字符串追加字符串str
c str 返回C格式字符串
find + npos 从字符串pos位置开始往后找字符c,返回该字符在字符串的位置
rfind 从字符串pos位置开始往后找字符c,返回该字符在字符串的位置
substr 在str从pos位置开始,截取n个字符,然后将其返回
void test6()
{
	string s("hello");
	s.push_back(' ');   //在s后插入空格
	s.append("world");  //在s后追加一个字符"world"
	s += 'h';           // 在str后追加一个字符'h'       
	s += "it";          // 在str后追加一个字符串"it"    
	cout << s << endl;
	cout << s.c_str() << endl; //以C语言的方式打印字符串

	//获取file的后缀
	string file("exe.cpp");
	string  pos =file.substr( file.rfind('.'),file.size()-file.rfind('.'));
	cout << pos << endl;
}

(5)string类非成员函数

函数 功能说明
operator+ 尽量少用,因为传值返回,会发生浅拷贝
operator>> 输入运算符重载
operator<< 输出运算符重载
getline 获取一行字符串
relational operators 大小比较

以上表格中的所有内容呢,就是string类的一些常用接口,我们需要熟练使用这些接口,并且能够实现,下篇博客写string的实现。

发布了31 篇原创文章 · 获赞 2 · 访问量 826

猜你喜欢

转载自blog.csdn.net/qq_42430426/article/details/98202695
今日推荐