admin管理员组文章数量:1611947
//下面这个程序很有意思,看看发现到第二次的时候svec里面不是1000个元素,这实际上是resize()函数在作怪!但是最后一步中的capacity如何为3000多的我自己也不太清//楚,如果有知道的可以给我留言:
#include <vector>
#include <string>
#include <iostream>
using namespace std;
int main()
{
vector<string> svec;
svec.reserve(1024);
int n=0;
while(n++<256)
{
string tmp;
svec.push_back(tmp);
}
cout<<"the last size : "<<svec.size()<<endl;
cout<<"the capasity before resize : "<<svec.capacity()<<endl;
svec.resize(svec.size()+svec.size()/2);
cout<<"after resize of the svec:"<<svec.size()<<endl;
cout<<"after read words, capacity : "<<svec.capacity()<<endl;
cout<<endl;
while(n++<1000)
{
string tmp;
svec.push_back(tmp);
}
cout<<"n= "<<n<<endl;
cout<<"the last size : "<<svec.size()<<endl;
cout<<"the capasity before resize : "<<svec.capacity()<<endl;
svec.resize(svec.size()+svec.size()/2);
cout<<"after resize of the svec:"<<svec.size()<<endl;
cout<<"after read words, capacity : "<<svec.capacity()<<endl;
cout<<endl;
while(n++<1048)
{
string tmp;
svec.push_back(tmp);
}
cout<<"the last size : "<<svec.size()<<endl;
cout<<"the capasity before resize : "<<svec.capacity()<<endl;
svec.resize(svec.size()+svec.size()/2);
cout<<"after resize of the svec:"<<svec.size()<<endl;
cout<<"after read words, capacity : "<<svec.capacity()<<endl; //最后这一步的capacity()为什么不是4096???
}
版权声明:本文标题:STL中的resize函数与capacity函数 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1728622289a1166504.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论