Effective STL 17 Use "the swap trick" to trim excess capacity

来源:互联网 发布:应届生程序员面试题 编辑:程序博客网 时间:2024/06/05 10:34

for vector

vector<int>(v.begin(), v.end()).swap(v);

for string

string(s.begin(), s.end()).swap(s);

both to clear a container and to reduce its capacity to the minmum your implementation offers.

vector<int>().swap(v);string().swap(s);