public member function
<deque>

std::deque::shrink_to_fit

void shrink_to_fit();
Shrink to fit
Requests the container to reduce its memory usage to fit its size.

A deque container may have more memory allocated than needed to hold its current elements: this is because most libraries implement deque as a dynamic array that can keep the allocated space of removed elements or allocate additional capacity in advance to allow for faster insertion operations.

This function requests that the memory usage is adapted to the current size of the container, but the request is non-binding, and the container implementation is free to optimize its memory usage otherwise.

Note that this function does not change the size of the container (for that, see resize instead).

Parameters

none

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// deque::shrink_to_fit
#include <iostream>
#include <deque>

int main ()
{
  std::deque<int> mydeque (100);
  std::cout << "1. size of mydeque: " << mydeque.size() << '\n';

  mydeque.resize(10);
  std::cout << "2. size of mydeque: " << mydeque.size() << '\n';

  mydeque.shrink_to_fit();

  return 0;
}


Output:
1. size of mydeque: 100
2. size of mydeque: 10

Complexity

At most, linear in the container size.

Iterator validity

No changes.

Data races

The container is modified.
No contained elements are accessed: concurrently accessing or modifying them is safe.

Exception safety

Basic guarantee: if an exception is thrown, the container is in a valid state.

See also