获取string的尺寸和已分配的内存空间大小
在C++中,获取字符串分配的内存大小,直接上代码…
#include<iostream>
using namespace std;
int main()
{
//获取string的尺寸和已分配内存空间的尺寸
string empty;
sring small = "hello";
string large = "获取string的尺寸和已分配内存空间的尺寸";
cout << "empty size is " << empty.size() << endl;
cout << "small size is " << small.size() << endl;
cout << "large size is " << large.size() << endl;
cout << "empty capacity is" << empty.capacity() << endl; //在不同的编译器里,初始的空间不一定
cout << "small capacity is" << small.capacity() << endl;
cout << "large capacity is" << larege.capacity() << endl;
//初始化分配内存时,按照n*16 - 1 的规则
empty.resize(80); // 16*6 =96
cout << "empty capacity is " << empty.capacity() << endl;
for(int i = 1; i < 50; i++)
{
empty += 'a';
}
return 0;
}
本文介绍了在C++中如何获取string对象的尺寸和已分配的内存空间大小,通过示例代码展示了具体的操作方法。
1127

被折叠的 条评论
为什么被折叠?



