In C++, std::deque::shrink_to_fit() asks a std::deque to give back memory that it does not need. It is useful after you removed many elements and want to reduce memory use.
It was added in C++11. It is only a request: the library is free to ignore it. The elements and the size never change. Include <deque> to use it.
This is a member of std::deque<T>, a double-ended queue. A deque is fast at adding and removing at both ends, and it allows access by index like std::vector. Unlike a vector, its elements are stored in several blocks, not in one contiguous block.
Syntax
Call shrink_to_fit with empty brackets. It takes no arguments.
C++
d.shrink_to_fit();
Call
What it does
d.shrink_to_fit()
Requests that unused memory be released. The elements stay as they are.
A deque has no capacity() function, so you cannot measure the result directly. The call is a hint, not a promise.
Library declarations
C++
void shrink_to_fit(); // since C++11
Parameters
Parameter
Description
None
shrink_to_fit() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
Nothing
shrink_to_fit() returns void. The values and size() do not change.
C++ Examples
Example 1 — Call shrink_to_fit() After Removing Many Elements
Make a big deque small, then ask it to release memory. The values and size are not changed.