笔试题 -- vector内存空间释放及vector数据清空
#include #include #include #include using namespace std;int main(){ vector vecStr; cout << "vecStr.size() = " << vecStr.size() << endl; cout << "vecStr.capacity() = " << vecStr.capacity() << endl; cout << "---------------\n"; vecStr.push_back("a"); cout << "vecStr.size() = " << vecStr.size() << endl; cout << "vecStr.capacity() = " << vecStr.capacity() << endl; cout << "---------------\n"; //当vector空间用完,再插入值时,vector内存空间会以1.5或2倍增长 vecStr.push_back("b"); vecStr.push_back("c"); cout << "vecStr.size() = " << vecStr.size() << endl; cout << "vecStr.capacity() = " << vecStr.capacity() << endl; cout << "---------------\n"; //只清空vector,未释放vector内存 vecStr.clear(); cout << "vecStr.size() = " << vecStr.size() << endl; cout << "vecStr.capacity() = " << vecStr.capacity() << endl; cout << "---------------\n"; //释放vector内存 vector().swap(vecStr); cout << "vecStr.size() = " << vecStr.size() << endl; cout << "vecStr.capacity() = " << vecStr.capacity() << endl; cout << "---------------\n"; return 0;}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~