deque shrink_to_fit in C++ STL

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// the deque::shrink_to_fit()
#include <bits/stdc++.h>
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;
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// the deque::shrink_to_fit()
#include <bits/stdc++.h>
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;
}

chevron_right


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

All powers are within you You can do anything and everything Believe in that

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.