服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++之string类对象的容量操作详解

C++之string类对象的容量操作详解

2021-12-24 14:38lxkeepcoding C/C++

通过在网站上的资料搜集,得到了很多关于string类对象的容量操作,通过对这些资料的整理和加入一些自己的代码,希望能够给你带来帮助

前言

hello,大家好,今天这期文章我们用来介绍string类对象的容量操作。希望对大家有所帮助,让我们开始吧。

 

1. size返回字符串的有效长度

返回字符串的长度,以字节为单位。这是符合字符串内容的实际字节数,不一定等于它的存储容量。

注意:返回的长度不包括\0哦。

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

int main()
{
	string s1 = "hello,word";
	cout << s1.size() << endl;
	return 0;
}

C++之string类对象的容量操作详解

 

2. length 返回字符串的有效长度

返回字符串的长度,以字节为单位。这是符合字符串内容的实际字节数,不一定等于它的存储容量。

length和size是同义的,功能相同。

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

int main()
{
	string s2 = "hello,word";
	cout << s2.length() << endl;
	return 0;
}

C++之string类对象的容量操作详解

 

3. capacity 返回总空间的大小

返回当前为字符串分配的存储空间的大小,以字节表示。这个容量不一定等于字符串长度。它可以等于或大于,额外的空间允许对象在向字符串添加新字符时优化其操作。注意,这个容量并不假设字符串的长度有限制。当这个容量用完并且需要更多的容量时,对象会自动扩展它(重新分配它的存储空间)。

capacity返回的值一般会比size大。也就是说在开辟空间的时候,是有开辟额外的空间的。

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

int main()
{
	string s3 = "hello,word";
	cout << s3.capacity() << endl;
	return 0;
}

C++之string类对象的容量操作详解

 

4. empty 检测是否为空串

返回字符串是否为空(即它的长度是否为0)。这个函数不会以任何方式修改字符串的值。如果字符串长度为0则为True,否则为false。

这个函数是检查字符串是否为空串的函数,当为空串时,返回值为真,输出值为1,如果不是看出,返回值为假,输出值为0.

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

int main()
{
	string s4;
	cout << s4.empty() << endl;
	return 0;
}

C++之string类对象的容量操作详解

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

int main()
{
	string s4="hello,word";
	cout << s4.empty() << endl;
	return 0;
}

C++之string类对象的容量操作详解

 

5. clear 清空有效字符

删除字符串的内容,该字符串变成一个空字符串(长度为0个字符)

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

int main()
{
	string s5="hello,word";
	s5.clear();
	cout << s5 << endl;
	return 0;
}

C++之string类对象的容量操作详解

 

6. resize 修改个数并填充

将字符串的长度调整为n个字符。如果n小于当前字符串的长度,则将当前值缩短到第n个字符,删除第n个字符以外的字符。如果n大于当前字符串长度,延长最后插入当前内容尽可能多的字符需要达到的大小n。如果指定c, c的新元素初始化复制,否则,他们初始化值字符(null字符)。

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

int main()
{
	string s6 = "hello,word";
	cout << s6.capacity() << endl;

	s6.resize(8);
	cout << s6 << endl;
	cout << s6.capacity() << endl;

	s6.resize(15);
	cout << s6.capacity() << endl;
	cout << s6 << endl;

	s6.resize(20,'m');
	cout << s6.capacity() << endl;
	cout << s6 << endl;

	return 0;
}

C++之string类对象的容量操作详解

 

7. reserve 为字符串预留空间

请求将字符串容量调整到计划的大小更改,最大长度为n个字符。
如果n大于当前字符串的容量,该函数将使容器的容量增加到n个字符(或更大)。在其他所有情况下,它被视为一个非绑定请求来缩小字符串的容量:容器实现可以自由地进行优化,让字符串的容量大于n。这个函数对字符串长度没有影响,也不能改变字符串的内容。

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

int main()
{
	string s7 = "hello,word";
	cout << s7.capacity() << endl << endl;

	s7.reserve(50);
	cout << s7.capacity() << endl << endl;

	s7.reserve(100);
	cout << s7.capacity() << endl << endl;
	return 0;
}

C++之string类对象的容量操作详解

 

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注服务器之家的更多内容!

原文链接:https://blog.csdn.net/weixin_53472920/article/details/119962617

延伸 · 阅读

精彩推荐