The deque::shrink_to_fit() is a built-in function in C++ STL which reduces the capacity of the container to fit its size and destroys all elements beyond the capacity. This function does not reduce the size of the container. It is used when a container has been allocated more memory than it needed then this function makes free that amount of memory which has been allocated extra.
Parameters: This function does not accept any parameter.
Return value: The function does not return anything.
Below programs illustrate the above function:
Deque size initially: 10 Deque elements are: 0 1 2 3 4 0 0 0 0 0 Deque size after resize(7): 7 Deque elements after resize(7) are: 0 1 2 3 4 0 0 0 0 0 Deque size after shrink_to_fit(): 7 Deque elements after shrink_to_fit() are: 0 1 2 3 4 0 0 0 0 0
Size of d is : 100 Size of d after resize is : 20
Note: shrink_to_fit() function is very useful in case of vector where size of container is changing fluently.
- deque::clear() and deque::erase() in C++ STL
- deque::pop_front() and deque::pop_back() in C++ STL
- deque::front() and deque::back() in C++ STL
- deque::empty() and deque::size() in C++ STL
- deque::emplace_front() and deque::emplace_back() in C++ STL
- deque::begin() and deque::end in C++ STL
- deque::at() and deque::swap() in C++ STL
- deque get_allocator in C++ STL
- deque crend in C++ STL
- deque cbegin() in C++ STL
- deque emplace in C++ STL
- deque crbegin in C++ STL
- deque::push_back() in C++ STL
- deque::push_front() in C++ STL
- deque::operator= and deque::operator in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.