C++ std::string shrink_to_fit() Function

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

Definition

In C++, std::string::shrink_to_fit() asks the string to reduce unused capacity so the buffer is closer to size().

The request is non-binding: the library may keep a larger capacity. Include <string>. (C++11)

This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.

Syntax

Call after you no longer need a large spare buffer.

C++
s.shrink_to_fit();
CallWhat it does
s.shrink_to_fit()Non-binding request to shrink capacity toward size.

After a large temporary growth, clear() sets size to 0 but often keeps capacity. shrink_to_fit() is the usual follow-up when you want to hint that spare room can go away.

Library declarations
C++
void shrink_to_fit();

Parameters

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

Return Value

Return ValueDescription
voidNo return value. Inspect capacity() to see what happened.

C++ Examples

Example 1 — Basic shrink_to_fit()

Grow, clear, then request a shrink.

C++
#include <iostream>
#include <string>

int main() {
    std::string s;
    s.reserve(200);
    s = "ok";
    std::cout << "cap before: " << s.capacity() << '\n';
    s.shrink_to_fit();
    std::cout << "cap after:  " << s.capacity() << '\n';
    std::cout << "size: " << s.size() << '\n';
    return 0;
}

How It Works

  1. We reserved a large buffer, then stored only "ok".
  2. shrink_to_fit() asks to drop spare capacity.
  3. Many libraries shrink close to size; yours might keep a little extra—that is allowed.

Example 2 — Shrink After clear()

Clear leaves capacity; shrink may reclaim it.

C++
#include <iostream>
#include <string>

int main() {
    std::string s(100, 'x');
    std::cout << "grown cap: " << s.capacity() << '\n';
    s.clear();
    std::cout << "after clear cap: " << s.capacity() << '\n';
    s.shrink_to_fit();
    std::cout << "after shrink cap: " << s.capacity() << '\n';
    return 0;
}

How It Works

  1. Constructing 100 characters forces a sizable buffer.
  2. clear() sets size to 0 but often keeps capacity.
  3. shrink_to_fit() may then reduce capacity (exact result varies).

Example 3 — Text Stays the Same

Confirm shrink does not erase characters.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "keep me";
    s.reserve(80);
    s.shrink_to_fit();
    std::cout << s << '\n';
    std::cout << "size: " << s.size() << '\n';
    return 0;
}

How It Works

  1. Spare capacity is requested away, not the characters.
  2. The printed text is unchanged.
  3. Size remains 7.

Frequently Asked Questions

It asks the library to reduce capacity closer to size(). The library may ignore the request.
No. It is non-binding. Always treat a smaller capacity as a bonus, not a promise.
No. Only capacity may change.
After a temporary large buffer is no longer needed, especially after clear() or after building a much shorter final string.
C++11.

Next: size()

Keep going through the string functions. Return how many characters the string currently holds.

size() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

14 people found this page helpful