If I use .reserve(items) on a vector, the vector will allocate enough memory for my guess of the number of items that I’ll need.
If I later on use .clear(), will that just clear the vector or save my earlier defined reserve?
thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It is specified that
std::vector<T>::clear()affects the size. It might not affect the capacity. For resetting the capacity, use the swap trick:Note: Since this old answer is still getting upvotes (thus people read it), I feel the need to add that C++11 has added
std::vector<...>::shrink_to_fit(), which requests the vector to remove unused capacity.