C++ std::deque shrink_to_fit() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::deque

Definition

In C++, std::deque::shrink_to_fit() asks a std::deque to give back memory that it does not need. It is useful after you removed many elements and want to reduce memory use.

It was added in C++11. It is only a request: the library is free to ignore it. The elements and the size never change. Include <deque> to use it.

This is a member of std::deque<T>, a double-ended queue. A deque is fast at adding and removing at both ends, and it allows access by index like std::vector. Unlike a vector, its elements are stored in several blocks, not in one contiguous block.

Syntax

Call shrink_to_fit with empty brackets. It takes no arguments.

C++
d.shrink_to_fit();
CallWhat it does
d.shrink_to_fit()Requests that unused memory be released. The elements stay as they are.

A deque has no capacity() function, so you cannot measure the result directly. The call is a hint, not a promise.

Library declarations
C++
void shrink_to_fit();   // since C++11

Parameters

ParameterDescription
Noneshrink_to_fit() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
Nothingshrink_to_fit() returns void. The values and size() do not change.

C++ Examples

Example 1 — Call shrink_to_fit() After Removing Many Elements

Make a big deque small, then ask it to release memory. The values and size are not changed.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<int> d(1000, 7);

    d.erase(d.begin() + 3, d.end());
    d.shrink_to_fit();

    std::cout << "size: " << d.size() << '\n';
    for (int n : d) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. d(1000, 7) creates 1000 elements, each with the value 7.
  2. erase(d.begin() + 3, d.end()) removes all but the first three elements. The deque may still hold extra memory.
  3. shrink_to_fit() asks it to release that memory. The size is still 3 and the values are the same.

Example 2 — shrink_to_fit() After clear()

clear() may keep memory. Follow it with shrink_to_fit() to ask for it back.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<int> d;

    for (int i = 0; i < 100; ++i) {
        d.push_back(i);
    }

    d.clear();
    d.shrink_to_fit();

    std::cout << std::boolalpha;
    std::cout << "empty: " << d.empty() << '\n';
    return 0;
}

How It Works

  1. The loop fills the deque with 100 numbers, so it needs a good amount of memory.
  2. clear() removes all elements, but the library may keep the memory for later use.
  3. shrink_to_fit() then requests that the memory be freed. The deque is empty either way, so empty() prints true.

Example 3 — The Deque Still Works Afterwards

After the request, you can keep using the deque like before.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<int> d = {5, 6, 7, 8};

    d.pop_front();
    d.shrink_to_fit();
    d.push_back(9);

    std::cout << "front: " << d.front() << '\n';
    std::cout << "back: " << d.back() << '\n';
    std::cout << "size: " << d.size() << '\n';
    return 0;
}

How It Works

  1. pop_front() removes 5, so the deque holds 6 7 8.
  2. shrink_to_fit() does not change any value. It also does not stop you from adding more elements.
  3. push_back(9) works normally. The deque is 6 7 8 9, so front() is 6, back() is 9, and the size is 4.

Frequently Asked Questions

It asks the deque to release unused memory. The values and the size stay the same.
No. It is a non-binding request, so the library may ignore it. Do not depend on it for correctness.
C++11. Any modern compiler supports it.
No. A deque has no capacity() and no reserve(), so you cannot read how much memory is held. shrink_to_fit() is the only hint you can give.
Yes. If memory is released, iterators, pointers, and references to elements may become invalid. Get new ones after the call.

Next: size()

Get the number of elements in the deque with std::deque.

size() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful