C++ std::vector shrink_to_fit() Function

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

Definition

In C++, std::vector::shrink_to_fit() asks the vector to reduce its capacity() closer to its size(), so spare memory can be released.

It is a non-binding request added in C++11: the library may ignore it. Include <vector> to use it.

This is a member of std::vector<T>, a dynamic contiguous array. Elements sit in one block, so v[i] and data() are fast. Growing past capacity() reallocates and can invalidate iterators, references, and pointers. Prefer push_back / pop_back at the end; middle inserts shift elements.

Syntax

Call shrink_to_fit with empty brackets. It takes no arguments.

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

A vector 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 vector small, then ask it to release memory. The values and size are not changed.

C++
#include <vector>
#include <iostream>

int main() {
    std::vector<int> v(1000, 7);

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

    std::cout << "size: " << v.size() << '\n';
    for (int n : v) {
        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 vector 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 <vector>
#include <iostream>

int main() {
    std::vector<int> v;

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

    v.clear();
    v.shrink_to_fit();

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

How It Works

  1. The loop fills the vector 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 vector is empty either way, so empty() prints true.

Example 3 — The Vector Still Works Afterwards

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

C++
#include <vector>
#include <iostream>

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

    v.pop_back();
    v.shrink_to_fit();
    v.push_back(9);

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

How It Works

  1. pop_back() removes the last element so the vector shrinks from the back.
  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 vector is 6 7 8 9, so front() is 6, back() is 9, and the size is 4.

Frequently Asked Questions

It asks the vector 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 vector 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 vector with std::vector.

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.

9 people found this page helpful