【C++】:string类的基本使用

引言

string 就是我们常说的"串",它是一种字符数组,只不过这个数组具备扩容,增删查改等功能。string类在我们日常生活中是十分常用的,并且在笔试,面试中也经常出现,它是学习C++的不可缺少的一部分。

string类大概有120个函数接口,注意下面只讲解最常用的接口,想要了解更多,前往https://legacy.cplusplus.com/reference/string/string/网站里浏览。

注意:使用string类时要包含 < string >

一,string类对象的常见构造

在这里插入图片描述

代码演示:

void test_string1()
{
	string s1;//无参默认构造

	string s2("hello world");//用字符串构造

	//从str中的pos下标位置,拷贝len个字符
	string s4(s2, 3, 5);
	
	//不传第三个参数,默认拷贝到结尾,缺省参数npos是整型最大值
	string s5(s2, 3);

	string s3(s2);//拷贝构造
}

二,string类对象的容量操作

在这里插入图片描述

代码演示1:

// size/clear/resize
void Test_string2()
{
	//注意:string类对象支持直接用cin和cout进行输入和输出
	string s("hello, world!!!");
	
	//计算字符个数,不算\0
	cout << s.size() << endl;
	cout << s.length() << endl;
	
	//计算容量,不算\0
	//capacity比实际空间少一个,有一个多的是预留给\0的
	cout << s.capacity() << endl;
	cout << s << endl;

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

	// 将s中有效字符个数增加到10个,多出位置用'a'进行填充
	//当传的值大于size时,此时会影响capacity+size,两个都会增加
	// “aaaaaaaaaa”
	s.resize(10, 'a');
	cout << s.size() << endl;
	cout << s.capacity() << endl;

	// 将s中有效字符个数增加到15个,多出位置用缺省值'\0'进行填充
	// "aaaaaaaaaa\0\0\0\0\0"
	// 注意此时s中有效字符个数已经增加到15个
	s.resize(15);
	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;
}

代码演示1:

//测试reserve
void test_string3()
{
	string s;
	
	//一般用在知道需要多少空间,提前开好
	//这样可以避免每次都要开空间,提升效率
	s.reserve(100);

	string s1("111111111");
	cout << s1.capacity() << endl;//15

	//扩容
	//reserve只影响capacity,不影响size,即不改变里面的数据
	s1.reserve(100);
	cout << s1.capacity() << endl;//111

	//缩容
	//传的值比当前的capacity小时,
	// vs一般不缩容,g++会缩
	s1.reserve(10);
	cout << s1.capacity() << endl;//15
}

注意:

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

三,string类对象的访问及遍历操作

string类对象的访问及遍历有三种方式:

1. 迭代器:begin()+end()
现在我们暂时把迭代器理解成像指针一样的东西,但是不一定是指针,后面会细讲:
begin返回第一个数据位置的iterator;
end返回最后一个数据的下一个位置(\0)的iterator;
在这里插入图片描述

2. for+[]
这是一个[ ]运算符重载。利用下面的模拟代码来说明几个问题:
(1) 引用返回的作用:一是减少拷贝,二是修改返回对象。
(2) 为什么可以用引用返回:_str[i]出了作用域还在,因为_str开辟在堆上,它返回的是堆上的一个字符的引用别名。
(3) 重载的底层也是用assert断言的,只要下标越界直接终止报错。

//大概模拟string类说明几个问题
class string
{
public:
	//引用返回
    //1.减少拷贝
    //2.修改返回对象  //s1[0] = 'x';
	char& operator[](int i)
	{
		assert(i < _size);
		return _str[i];//返回的是第i个字符的别名
     //用引用返回:_str[i]出了作用域还在,因为_str开辟在堆上
     //它返回的是堆上的一个字符的引用别名
	}

private:
	char* _str;
	int _size;
	int _capacity;
};

3. 范围for

注意:string遍历时使用最多的还是for+下标 或者 范围for(C++11后才支持)

begin()+end()大多数使用在需要使用STL提供的算法操作string和后面的list容器,set容器,比如:采用reverse逆置string,使用sort按字典序排序(按ASCII码值排序)。
在这里插入图片描述

代码演示:

void Teststring4()
{
	string s("hello Bit");
	
	// 3种遍历方式:
	// 需要注意的以下三种方式除了遍历string对象,还可以遍历是修改string中的字符,
	// 另外以下三种方式对于string而言,第一种使用最多
	
	// 1. for+operator[]
	for (size_t i = 0; i < s.size(); ++i)
		cout << s[i] << endl;

	// 2.迭代器
	//string::iterator it = s.begin();
	//iterator 是typedef在类域中的,类似域内部类,所以在域外使用时要指定类域
	
	auto it1 = s1.begin();
	while (it != s.end())
	{
		cout << *it << endl;
		++it;
	}

	// string::reverse_iterator rit = s.rbegin();
	// C++11之后,直接使用auto定义迭代器,让编译器推到迭代器的类型
	auto rit = s.rbegin();
	while (rit != s.rend())
	{
		cout << *rit << endl;
	}

	// 3.范围for
	//自动取出s中的数据赋给ch,自动判断结束,自动++。
	//其实底层就是迭代器。
	for (auto ch : s)
	{
		cout << ch << endl;
	}
}

使用sort按字典序排序(按ASCII码值排序):

void test_string5()
{
	string s1("hello world");
	cout << s1 << endl;

	//按字典序排序(按ASCII码值排序)
	//用排序函数sort [first last)左闭右开,last传的不是有效数据
	//sort(s1.begin(), s1.end());

	//第一个和最后一个不参与排序
	//sort(++s1.begin(), --s1.end());

	//前5个排序  [0,5)
	sort(s1.begin(), s1.begin() + 5);
	cout << s1 << endl;
}

四,string类对象的修改操作

在这里插入图片描述

  1. 插入(拼接)方式:push_back append operator+=
  2. 正向和反向查找:find() + rfind()
  3. 截取子串:substr()
  4. 删除:erase

代码演示1:

void test_string6()
{
	string s1("hello world");
	cout << s1 << endl;

	s1.push_back('x');//一个字符一个字符尾插
	cout << s1 << endl;

	s1.append(" yyyyyyy!!");//尾插一个字符串
	cout << s1 << endl;

	string s2("22222");

	//直接尾插
	s1 += 'aaa';
	s1 += 'd';
	s1 += s2;

	cout << s1 << endl;
}

代码演示2:

void test_string7()
{
    // 获取file的后缀
	string file("string.cpp");
	
	//从后往前找'.'
	size_t pos = file.rfind('.');
	string suffix(file.substr(pos, file.size() - pos));
	cout << suffix << endl;

	// npos是string里面的一个静态成员变量
	// static const size_t npos = -1;

	// 取出url中的域名
	string url("http://www.cplusplus.com/reference/string/string/find/");
	cout << url << endl;
	size_t start = url.find("://");
	
	if (start == string::npos)
	{
		cout << "invalid url" << endl;
		return;
	}
	
	start += 3;
	size_t finish = url.find('/', start);
	string address = url.substr(start, finish - start);
	cout << address << endl;

	// 删除url的协议前缀
	pos = url.find("://");
	url.erase(0, pos + 3);
	cout << url << endl;
}

注意:

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

五,string类非成员函数

![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/0d01f1518e0e4654a84bd21e221afb38.png

代码演示1:

//+运算符
void test_string8()
{
	string s1 = "hello";
	string s2 = "world";

	string ret1 = s1 + s2;
	cout << ret1 << endl;

	string ret2 = s1 + "xxxxx";
	cout << ret2 << endl;

	string ret3 = "xxxxx" + s1;
	cout << ret3 << endl;

	//按字典序比较
	cout << (s1 < s2) << endl;
}

代码演示2:

//getline
int main()
{
	string str;

	string str2;
	//如何停止输入?
	//ctrl+c
	//ctrl+z+空格
	//	while (cin >> str2)
	//	{
	//		cout << str2 << endl;
	//	}

	//cin 遇到空格或者换行会停止提取
	//cin >> str;

	//获取一行包含空格的字符串
	getline(cin, str);

	int pos = str.rfind(' ');
	cout << str.size() - (pos + 1) << endl;

	return 0;
}

六,整形与字符串的转换

int main()
{
	int x = 0, y = 0;
	cin >> x >> y;
	
	//to_string:整形转字符串
	string str = to_string(x + y);
	cout << str << endl;
	
	//stoi:字符串转整形
	int aa = stoi(str);
	cout << aa << endl;

	return 0;
}

七,string类中的insert,assign,erase,replace

注意:
insert,erase,replace要慎用,因为string的底层其实就是数组顺序表,在pos位置插入,删除和替换时,需要挪动数据,时间复杂度为O(N),效率不高。

代码演示1:

// insert/assign
void test_string9()
{
	string s1("hello world");
	cout << s1 << endl;

	//变相赋值,会把原来的字符串全部替换掉
	s1.assign("111111");
	cout << s1 << endl;

	//效率不高 O(N)
	//头插一个字符串
	string s2("hello world");
	
	//下标位置  字符串
	s2.insert(0, "aaaaaa");
	cout << s2 << endl;

	//头插一个字符
	//下标位置  插入的个数 字符
	s2.insert(0, 1, 'f');
	cout << s2 << endl;
}

代码演示2:

// erase/replace
void test_string10()
{
	string s1("hello world");
	cout << s1 << endl;

	//效率不高 O(N)
	//头删字符
	s1.erase(0, 2);//下标位置  字符个数
	cout << s1 << endl;

	//效率不高 O(N)
	//替换部分字符串
	string s2("hello world");
	
	//下标位置 字符个数 替换内容 
	s2.replace(5, 1, "%20");
	cout << s2 << endl;
}
  • 77
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 47
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值