【STL】string类(中)

目录


1,rbegin 和 rend

具体详情:cplusplus.com/reference/string/string/rbegin/

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

int main()
{
	string s1("hello world");

	string::reverse_iterator it = s1.rbegin();
	while (it != s1.rend())
	{
		cout << *it << " ";
		it++;
	}

	return 0;
}

其实也就是逆置打印字符串;

而且缺点也很明显,类型太长了不便于书写;

所以 auto 的好用之处就体现出来了,自动帮我们推算类型;

int main()
{
	string s1("hello world");

	//string::reverse_iterator it = s1.rbegin();
	auto it = s1.rbegin();
	while (it != s1.rend())
	{
		cout << *it << " ";
		it++;
	}

	return 0;
}

这样就更 OK 了;

还有一个冷门不常用的,就是 crengin 代替 rengin ,crend 代替 crend ;

int main()
{
	string s1("hello world");

	//string::reverse_iterator it = s1.crbegin();
	auto it = s1.crbegin();
	while (it != s1.crend())
	{
		cout << *it << " ";
		it++;
	}

	return 0;
}

2,reserve & capacity

reserve:更改容量,需要多少空间,提前开好即可

capacity:返回空间总大小

int main()
{
	string s1;
	string s2("hello world");

	//初始容量大小
	cout << s1.capacity() << endl;
	cout << s2.capacity() << endl;
	cout << endl;

	s1.reserve(20);
	s2.reserve(30);

	//当 n>容量大小
	cout << s1.capacity() << endl;
	cout << s2.capacity() << endl;
	cout << endl;

	s1.reserve(5);
	s2.reserve(5);

	//当 n<容量大小
	cout << s1.capacity() << endl;
	cout << s2.capacity() << endl;

	return 0;
}

小伙伴们会好奇,为什么扩容的容量不跟我们指定的容量相同;

那是因为编译器有自己的一套扩容机制;

int main()
{
	string s1;
	string s2("hello world");

	int doll = s1.capacity();
	cout << s1.capacity() << endl;
	int i = 1000;
	while (i--)
	{
		s1 += ' ';
		if (doll != s1.capacity())
		{
			cout << s1.capacity() << endl;
			doll = s1.capacity();
		}
	}
	return 0;
}

基本上是按 1.5 倍扩增的,除了刚开始的;

所以空间容量只会在这些值里面,就算是 16 也直接扩容至下一阶段 31,其实是 32,因为还有一个 ‘ \0 ‘ ;

当扩容之后的量大于当前的容量则扩大;

当扩容之后的量小于当前真实容量则不变,否则缩小;

string s2(“hello world”) 里面的 ” hello world ” 真实容量就是11;

3,max_size ( )

算出字符串所能开辟的最大空间

int main()
{
	string s1;
	string s2("hello world");

	cout << s1.max_size() << endl;
	cout << s2.max_size() << endl;

	return 0;
}

由上可得 string类所能开辟的最大空间都是一样的;

但是所能开辟的最大空间并不是真的能开辟,我们来看一段代码;

int main()
{
	string s1;
	string s2("hello world");

	cout << s2.capacity() << endl;
	cout << s2.max_size() << endl;
	 
	s2.reserve(s2.max_size());
	cout << s2.capacity() << endl;

	return 0;
}

上面开最大空间的容量的时候,运行直接崩溃的,根本开不出来; 

真实的话是开不出来的,这仅供参考,不必当真;

4,size()& resize

size():返回字符串有效长度

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

我们直接来看一段代码

int main()
{
	string s1;
	string s2("hello world");
	
	//打印有效字符长度和容量大小
	cout << s1.size() << " " << s1.capacity() << endl;
	cout << s2.size() << " " << s2.capacity() << endl;
	cout << endl;

	s1.resize(20);
	s2.resize(30);
	
	//有效字符对容量的影响
	cout << s1.size() << " " << s1.capacity() << endl;
	cout << s2.size() << " " << s2.capacity() << endl;
	cout << endl;

	s1.resize(5);
	s2.resize(8);
	//
	cout << s1.size() << " " << s1.capacity() << endl;
	cout << s2.size() << " " << s2.capacity() << endl;
	cout << endl;

	s1.reserve(100);
	s2.reserve(200);

	//容量对有效字符的影响
	cout << s1.size() << " " << s1.capacity() << endl;
	cout << s2.size() << " " << s2.capacity() << endl;


	return 0;
}

由上可得,有效字符 size 的长度会影响 capacity 容量,但是 capacity 容量的大小不会影响 有效字符 size ;

而且 字符串有效字符长度会随着 size 的变化而变化即使是缩小,但是容量不会改变,以后可以用于【删除数据,保留前 n 个】

1,void resize (size_t,char c)

扩增加尾插

int main()
{
	string s1;
	string s2("hello world");

	s1.resize(10, 'y');
	s2.resize(20,'x');
	cout << s2 << endl;
	cout<< s1 << endl;

	return 0;
}

这个我们以后可以用作给字符串赋值和初始化;

5,push_back & append

push_back :在字符串后尾插字符c

append:在字符串后追加一个字符串

直接上代码:

int main()
{
	string s1;
	string s2("hello world");

	s1.push_back('x');
	s2.push_back('y');
	cout << s1 << endl;
	cout << s2 << endl;
	cout << endl;

	s2.append("hello world");
	s2.append("hello wprld");
	cout << s1 << endl;
	cout << s2 << endl;

	return 0;
}

1,追加字符串范围

string& append(inputiterator first,inputiterator last);

int main()
{
	string s1("abcdefg");
	string s2("hello world");

	s1.append(s2.begin(), s2.end());
	cout << s1 << endl;
	cout << endl;

	string s3("abcdefg");
	s2.append(++s3.begin(), --s3.end());
	cout << s2 << endl;

	return 0;
}

直接范围也是可以的,还可以 ++,–;

int main()
{
	string s1;
	string s2("hello world");

	s1.append(s2,2,7);
	s2.append(10, 'x');

	cout << s1 << endl;
	cout << s2 << endl;

	return 0;
}

 指定也是可以的,用法有很多更 string类的用法类似,大家可以去查查文档的各种用法;

2,直接追加

int main()
{
	string s1;
	string s2("hello world");

	s1 += 'x';
	s2 += " abcdefg";
	cout << s1 << endl;
	cout << s2 << endl;

	return 0;
}

直接 追加也可以,更简便;

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

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

(0)
乘风的头像乘风管理团队
上一篇 2023年12月5日
下一篇 2023年12月5日

相关推荐