多说无益直接上代码!
#include<iostream>
//#include<string>
#include<vector>
using namespace std;
void test()
{
vector<int> v;
v.push_back(1);
size_t capacity = v.capacity();
cout << "v2:capacity():" << capacity << endl;
v.push_back(1);
for (int i = 0; i < 200; ++i)
{
v.push_back(i);
if (capacity!= v.capacity())
{
capacity = v.capacity();
cout << capacity << endl;
}
}
}
int main()
{
test();
return 0;
}
由此可见,当容量不够用的时候,大概是一个1.5倍的扩容。