#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v;
cout<<v.size()<<", "<< v.capacity()<<endl;
v.reserve(12);
for(int i = 0; i < 13; i++) // 超出vector本身容量后,会导致capacity容量增加一倍
v.push_back(i);
cout<<v.size()<<", "<< v.capacity()<<endl;
return 0;
}
编译运行:
[zcm@t #116]$make
g++ -g -c -o a.o a.c
g++ -g -o a a.o
[zcm@t #117]$./a
0, 0
13, 24
[zcm@t #118]$