STL中的resize函数与capacity函数

//下面这个程序很有意思,看看发现到第二次的时候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???
 }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值