Related Articles

# deque shrink_to_fit in C++ STL

• Last Updated : 19 Dec, 2018

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:

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
```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

 `// C++ program to illustrate``// the deque::shrink_to_fit()``#include ``using` `namespace` `std;`` ` `int` `main()``{`` ` `    ``// Initialized deque``    ``deque<``int``> d(10);`` ` `    ``for` `(``int` `i = 0; i < 5; i++)``        ``d[i] = i;`` ` `    ``// Initial deque``    ``cout << ``" Deque size initially: "` `<< d.size();`` ` `    ``cout << ``"\n Deque  elements are: "``;``    ``for` `(``int` `i = 0; i < 10; i++)``        ``cout << d[i] << ``" "``;`` ` `    ``// changes the size of the Deque``    ``// but does not destroys the elements``    ``d.resize(7);`` ` `    ``cout << ``"\n Deque size after resize(7): "``         ``<< d.size();`` ` `    ``cout << ``"\n Deque elements after resize(7) are: "``;``    ``for` `(``int` `i = 0; i < 10; i++)``        ``cout << d[i] << ``" "``;`` ` `    ``// Shrinks to the size``    ``// till which elements are``    ``// destroys the elements after 5``    ``d.shrink_to_fit();`` ` `    ``cout << ``"\n Deque size after shrink_to_fit(): "``         ``<< d.size();`` ` `    ``cout << ``"\n Deque elements after shrink_to_fit() are: "``;``    ``for` `(``int` `i = 0; i < 10; i++)``        ``cout << d[i] << ``" "``;`` ` `    ``return` `0;``}`
Output:
```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

 `// C++ program to illustrate``// the deque::shrink_to_fit()``#include ``using` `namespace` `std;`` ` `int` `main()``{``    ``// creating a deque``    ``deque<``int``> d(100);`` ` `    ``cout << ``"Size of d is : "` `<< d.size() << endl;`` ` `    ``// resizing``    ``d.resize(20);`` ` `    ``cout << ``"Size of d after resize is : "` `<< d.size() << endl;`` ` `    ``d.shrink_to_fit();``    ``return` `0;``}`
Output:
```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.

My Personal Notes arrow_drop_up