C++——string类

前言:哈喽小伙伴们,从这篇文章开始我们将进行若干个C++中的重要的类容器的学习。本篇文章将讲解第一个类容器——string。

目录

一.什么是string类

二.string类常见接口

1.string类对象的常见构造

 2.string类对象的容量操作

3. string类对象的访问及遍历操作

4.string类对象的修改操作

总结

一.什么是string类

C语言中,字符串是以’\0’结尾的一些字符的集合,为了操作方便,C标准库中提供了一些str系列的库函数,但是这些库函数与字符串是分离开的,其操作也并不能完全满足用户的期望。

而在C++的标准库中,默认存在一个string类,并且该类拥有很多的成员函数,来帮助我们更加方便的完成对字符串的一系列操作。

使用string类,需要包含头文件#include<string>。

二.string类常见接口

1.string类对象的常见构造

对于如何得到一个string类型的对象,有以下几种常见构造:

  • string()                                 构造空的string类对象,即空字符串
  • string(const char* s)          构造一个string类对象,其内容为s
  • string(size_t n,char c)        构造一个string类对象,其内容为n个c字符
  • string(const string&s)        拷贝构造函数,拷贝s字符串内容

实践代码如下:

#include<string>
#include<iostream>
using namespace std;

int main()
{
	string s0;
	string s1("hello world!");
	string s2(5,'c');
	string s3(s1);
	cout << s0 << endl;
	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
}

结果如下:

其中s0为空字符串,所以第一行即为空。

 2.string类对象的容量操作

如何得到string类对象的信息以及对对象的操作,有如下方法:

  • size                返回字符串有效字符长度
  • length            返回字符串有效字符长度
  • capacity         返回空间总大小
  • empty            检测字符串释放为空串,是返回true,否则返回false
  • clear              清空有效字符
  • reserve          为字符串预留空间**
  • resize            将有效字符的个数该成n个,多出的空间用字符c填充

 测试如下:

#include<string>
#include<iostream>
using namespace std;

int main()
{
	string s1("hello world!");
	s1.reserve(20);
	cout << s1.size() << endl;
	cout << s1.length() << endl;
	cout << s1.capacity() << endl;
	cout << s1.empty() << endl;
	s1.clear();
	cout << s1.empty() << endl;
	s1.resize(10, 'x');
	cout << s1 << endl;
	return 0;
}

结果如下: 

值得注意的是:

1. size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,一般情况下基本都是用size()。

2. clear()只是将string中有效字符清空,不改变底层空间大小。 

3.reserve(size_t res_arg=0):为string预留空间,不改变有效元素个数,当reserve的参数小于string的底层空间总大小时,reserver不会改变容量大小。 

3. string类对象的访问及遍历操作

如何对一个string类对象进行遍历操作,有如下方法:

  • operator[]             返回pos位置的字符,const string类对象调用
  • 范围for                  C++11支持更简洁的范围for的新遍历方式
  • 迭代器iterator         

operator[]对[]运算符的重载,实现数组下标的运算功能:

int main()
{
	string s1("hello world!");
	for (size_t i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << ' ';
	}
	return 0;
}

范围for在前边的文章中已经介绍过了:

int main()
{
	string s1("hello world!");
	for (char e : s1)
	{
		cout << e << ' ';
	}
	return 0;
}

可以认为是直接用一个临时变量e去一一访问s1字符串的字符

迭代器的作用是创建一个新的对象,可以遍历并选择序列中的一个对象,其关键字为iterator。

迭代器包含有若干方法,常用的有:

begin        获取一个字符串的首位字符

end           获取一个字符串的最后一个字符的下一个字符

用迭代器遍历字符串的方法为:

int main()
{
	string s1("hello world!");
	string::iterator it1 = s1.begin();
	while (it1 != s1.end())
	{
		cout << *it1 << ' ';
		it1++;
	}
	return 0;
}

这种方法,很类似于指针,但又与指针有很大不同,本文不做过多解释。

结果如下:

除此之外迭代器还可以反向遍历,需要关键字reverse_iterator,同时方法:

rbegin        获取一个字符串的最后一个字符的下一个字符

rend           获取一个字符串的首位符

int main()
{
	string s1("hello world!");
	string::reverse_iterator it1 = s1.rbegin();
	while (it1 != s1.rend())
	{
		cout << *it1 << ' ';
		it1++;
	}
	return 0;
}

 结果如下:

4.string类对象的修改操作

如何对一个string类对象进行各种修改操作,有如下方法:

  • push_back        在字符串后尾插字符c
  • append              在字符串后追加一个字符串
  • operator+=        在字符串后追加字符串str
  • c_str                  将C++格式的字符串转化为C语言格式的字符串
  • find + npos        从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置
  • rfind                   从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置
  • substr                在str中从pos位置开始,截取n个字符,然后将其返回

对于c_str在C语言格式下的字符串,其末尾都会有一个‘\0’用来统计该字符串的长度,而C++格式下的string字符串由于会有size方法来表示字符串长度,所以其字符串结尾不会有‘\0’

 使用案例如下:

int main()
{
	string s1 = "hello world";
	s1.push_back('!');
	cout << s1 << endl;
	s1.append(" hello");
	cout << s1 << endl;
	s1 += " C++";
	cout << s1 << endl;
	size_t pos1 = s1.find(' ');//从前往后找到s1中的' '字符并返回其下标位置
	if (pos1 != 'npos')
	{
		cout << s1.substr(pos1) << endl;//从pos位置开始截取字符
	}
	size_t pos2 = s1.rfind(' ');//从后往前找到s1中的' '字符并返回其下标位置
	if (pos1 != 'npos')
	{
		cout << s1.substr(pos2) << endl;//从pos位置开始截取字符
	}
	return 0;
}

结果如下:

总结

关于string类的用法到这里就分享完啦。

下篇文章将对string类各接口方法进行模拟实现讲解,敬请期待

最后希望能留下您的一键三连,我们下期再见!

版权声明:本文为博主作者:很楠不爱原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/2303_78442132/article/details/136421581

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
乘风的头像乘风管理团队
上一篇 2024年4月10日
下一篇 2024年4月10日

相关推荐