![]() |
VOOZH | about |
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. Syntax:
deque_name.shrink_to_fit()
Parameters: This function does not accept any parameter. Return value: The function does not return anything. Below programs illustrate the above function: Example-1
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
Example-2
Size of d is : 100 Size of d after resize is : 20
Time complexity: O(n). // n is the number of elements in the deque.
Space complexity: O(n).
Note: shrink_to_fit() function is very useful in case of vector where size of container is changing fluently.