#include <iostream>
#include <vector>
#include <memory>
#include <algorithm>
using namespace std;
int main()
{
vector<unique_ptr<int>> demo;
//demo.push_back(1); 编译错误
unique_ptr<int> up(new int(1));
//demo.emplace_back(up); //编译错误
demo.push_back(std::move(up));
cout << *demo[0] << endl;
//cout << *up <<endl; 程序崩溃
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
cout << "size:" << v.size() << endl; //5
cout << "capacity:" << v.capacity() << endl;//6
//清空方法1
v.clear();
cout << "after clear size:" << v.size() << endl;//0
cout << "after clear capacity:" << v.capacity() << endl;//6
//清空方法2
vector<int>().swap(v);
cout << "after swap size:" << v.size() << endl;//0
cout << "after swap capacity:" << v.capacity() << endl; //0
//清空方法3
v.clear();
v.shrink_to_fit();
cout << "after swap size:" << v.size() << endl;//0
cout << "after swap capacity:" << v.capacity() << endl;//0
return 0;
}
vector和unique_ptr及clear操作
最新推荐文章于 2023-09-07 00:22:14 发布