No, it doesn’t. The capacity of a vector never decreases. That isn’t mandated by the standard but it’s so both in standard library implementations of VC++ and g++. In order to set the capacity just enough to fit the size, use the famous swap trick
vector<T>().swap(foo);
In C++11 standard, you can do it more explicitly:
foo.shrink_to_fit();
Related Contents:
- How to find out if an item is present in a std::vector?
- vector size – 1 when size is 0 in C++
- Replace part of a string with another string
- What’s the most efficient way to erase duplicates and sort a vector?
- Is std::vector so much slower than plain arrays?
- How to enforce move semantics when a vector grows?
- C++ OpenMP Parallel For Loop – Alternatives to std::vector [closed]
- Best way to extract a subvector from a vector?
- How do I combine hash values in C++0x?
- When vectors are allocated, do they use memory on the heap or the stack?
- How does this “size of array” template function work? [duplicate]
- Is std::vector or boost::vector thread safe?
- How to avoid memory leaks when using a vector of pointers to dynamically allocated objects in C++?
- Vector erase iterator
- Compelling examples of custom C++ allocators?
- How to get std::vector pointer to the raw data?
- c++ vector size. why -1 is greater than zero
- Erasing from a std::vector while doing a for each?
- What is the best way to concatenate two vectors?
- How do I print the elements of a C++ vector in GDB?
- What is the difference between std::array and std::vector? When do you use one over other? [duplicate]
- How std::bind works with member functions
- How to read a binary file into a vector of unsigned chars
- how do you insert the value in a sorted vector?
- GCC C++ “Hello World” program -> .exe is 500kb big when compiled on Windows. How can I reduce its size?
- How to downsize std::vector?
- sizeof() a vector
- Why is it OK to return a ‘vector’ from a function?
- C++ : why bool is 8 bits long?
- Vector of Vectors to create matrix
- How to convert std::chrono::time_point to calendar datetime string with fractional seconds?
- Are vectors passed to functions by value or by reference in C++
- Is there a range class in C++11 for use with range based for loops?
- When is it necessary to use the flag -stdlib=libstdc++?
- Does std::vector *have* to move objects when growing capacity? Or, can allocators “reallocate”?
- Moving elements from std::vector to another one
- What happens if you increment an iterator that is equal to the end iterator of an STL container
- pop_back() return value?
- Search a vector of objects by object attribute
- std::string length() and size() member functions
- Compute Median of Values Stored In Vector – C++?
- How to navigate through a vector using iterators? (C++)
- Can we rely on the reduce-capacity trick?
- do I need to close a std::fstream? [duplicate]
- How to reliably get size of C-style array?
- How to print class object using operator
- Optimization of raw new[]/delete[] vs std::vector
- Erasing vector::end from vector
- Vector Iterators Incompatible
- Conversion from boost::shared_ptr to std::shared_ptr?