本文介绍了字符串的容量和大小不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是代码:
here is a code :
#include <iostream>
#include <string>
using namespace std;
int main() {
std::string s1 = "Hello";
cout << ".size() returns " << s1.size() << endl;
cout << ".capacity() returns " << s1.capacity() << endl;
s1.append("!");
cout << ".size() returns " << s1.size() << endl;
cout << ".capacity() returns " << s1.capacity() << endl;
return 0;
}
这是输出:
有时大小= 6,容量= 12....
为什么与众不同?
容量和大小有什么区别?
预先感谢.
This is an Output :
sometimes Size=6 and Capacity=12....
Why Its different?
What is different between Capacity and Size?
Thanks in advance.
推荐答案
std::string s1;
for (int i = 0; i < 100; ++i)
{
s1.append("");
cout << "Size = " << s1.size() << ", Capacity = " << s1.capacity() << endl;
}
这篇关于字符串的容量和大小不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!