Q: How to clear extra capacity of vector?
A: Use the shrink-to-fit technique.If you want to clear it all (no elements required but the vector lives because of scope), do this:Code:std::vector<int> myvector; myvector.reserve(10000); //use vector //don't need extra capacity? //clear it using shrink-to-fit technique: std::vector<int>(myvector).swap(myvector);For more information on this please refer the following link:Code:std::vector<int>().swap(myvector);
- Using vector and deque by Herb Sutter (GotW #54)
- Item 17: Use "the swap trick" to trim excess capacity by Scott Meyers (Effective STL, Item 17)


My Blogs :
Reply With Quote
Bookmarks