vector resize 错误用法

vector<int> v;
v.push_back(3);
v.push_back(3);
cout << v.size() << " " << v.capacity() << endl; //2 2

v.resize(4, 4); //3 3 4 4, 对已经初始化的位置不会重新进行构造, 所以resize(4,4) 前应该先clear() 或者 resize(0)即可
cout << v.size() << " " << v.capacity() << endl; //4 4
	
v.clear();
cout << v.size() << " " << v.capacity() << endl; //0 4

v.resize(0);
cout << v.size() << " " << v.capacity() << endl; //0 4

  

posted @ 2017-03-07 14:15  YaLing  阅读(2096)  评论(0编辑  收藏  举报