- resize() 或者 erase() 后 vector 的空间大小不会自动减小
int main()
{
vector<int> v;
for(int i = 0; i < 100000; ++i){
v.push_back(i);
}
cout << "size: " << v.size() << endl;
cout << "capacity: " << v.capacity() << endl;
cout << "-----------------------" << endl;
v.resize(10);
cout << "size: " << v.size() << endl;
cout << "capacity: " << v.capacity() << endl;
cout << "-----------------------" << endl;
vector<int>(v).swap(v);
cout << "size: " << v.size() << endl;
cout << "capacity: " << v.capacity() << endl;
return 0;
}
输出:
size: 100000
capacity: 131072
-----------------------
size: 10
capacity: 131072
-----------------------
size: 10
capacity: 10