-
November 12th, 2006, 09:05 AM
#1
STL General - How to clear extra capacity of vector?
Q: How to clear extra capacity of vector?
A: Use the shrink-to-fit technique.
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);
If you want to clear it all (no elements required but the vector lives because of scope), do this:
Code:
std::vector<int>().swap(myvector);
For more information on this please refer the following link:
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|