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???
 }



本文标签: 函数stlresizeCapacity