#include <iostream>
#include <string>
using namespace std;
#include <vector>
int main()
{
vector<int> v;
for(size_t i = 0; i < 10000; i++)
{
v.push_back(i);
}
cout << "size: " << v.size() << endl;
cout << "capacity: " << v.capacity() << endl;
v.resize(3);
cout << "size: " << v.size() << endl;
cout << "capacity: " << v.capacity() << endl;
// 巧用swap收缩内存
vector<int>(v).swap(v); // 匿名对象:执行完这一句话就会释放
cout << "size: " << v.size() << endl;
cout << "capacity: " << v.capacity() << endl;
return 0;
}
C++ 101 之 swap收缩容器
最新推荐文章于 2025-03-14 00:50:45 发布