public member function

std::deque::shrink_to_fit

<deque>
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 otherwise or ignore the request.

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

Constant.

Iterator validity

No changes.

See also