c++ vector容器自增长

时间:2022-11-06 04:21:39

#include <iostream>
#include <vector>


using namespace std;

 

int main()
{
  vector<int> vec1;
  for (int k=0;k<20;k++)
  {
    vec1.push_back(k);
    cout << "size:"<<vec1.size()<<" capacity:"<<vec1.capacity()<< endl;
  
  }

  system("pause");
  return 0;
}

 

===============================================

size:1   capacity:1
size:2    capacity:2
size:3    capacity:3
size:4    capacity:4
size:5    capacity:6
size:6    capacity:6
size:7    capacity:9
size:8    capacity:9
size:9    capacity:9
size:10    capacity:13
size:11    capacity:13
size:12    capacity:13
size:13    capacity:13
size:14    capacity:19
size:15    capacity:19
size:16    capacity:19
size:17    capacity:19
size:18    capacity:19
size:19    capacity:19
size:20    capacity:28
请按任意键继续. . .