vector拷贝构造和赋值拷贝时,容器容量和大小的关系
考虑3种情况下
- 拷贝构造
- 赋值拷贝(未指定reserve)
- 赋值拷贝(指定reserve)
代码实现:
using namespace std;
void showinfo(vector<int>& v) {
cout << "容量" << v.capacity()
<< "\t";
cout << "大小" << v.size()
<<"\t";
cout << endl;
cout << endl;
}
void test() {
vector<int> v1 = {1,2,3};
v1.reserve(5);
showinfo(v1);
cout << "拷贝构造:" << endl;
vector<int> v2(v1);
showinfo(v2);
cout << "赋值拷贝无参初始化vector:" << endl;
vector<int> v3;
v3 = v1;
showinfo(v3);
cout << "赋值拷贝初始化容量的vector:" << endl;
vector<int> v4;
v4.reserve(5);
v4 = v1;
showinfo(v4);
}