Skip to main content

std::string shrink_to_fit() method

// Non const version only
constexpr void shrink_to_fit();

Requests the removal of unused capacity.
It is a non-binding request to reduce capacity() to size().
It depends on the implementation whether the request is fulfilled.

Invalidation

If reallocation takes place, all pointers, references, and iterators are invalidated.

Parameters

(none)

Return value

(none)

Complexity

Linear in the size of the container - O(size()).

Exceptions

(none)

Example

Main.cpp
#include <iostream>
#include <string>

int main()
{
std::string s;
std::cout << "Size of std::string is " << sizeof s << " bytes\n"
<< "Default-constructed capacity is " << s.capacity()
<< " and size is " << s.size() << '\n';
for (int i=0; i<42; i++)
s.append(" 42 ");
std::cout << "Capacity after 42 appends is " << s.capacity()
<< " and size is " << s.size() << '\n';
s.clear();
std::cout << "Capacity after clear() is " << s.capacity()
<< " and size is " << s.size() << '\n';
s.shrink_to_fit();
std::cout << "Capacity after shrink_to_fit() is " << s.capacity()
<< " and size is " << s.size() << '\n';
}
Possible output
GCC output:
Size of std::string is 32 bytes
Default-constructed capacity is 15 and size 0
Capacity after 42 appends is 240 and size 168
Capacity after clear() is 240 and size 0
Capacity after shrink_to_fit() is 15 and size 0

clang output (with -stdlib=libc++):
Size of std::string is 24 bytes
Default-constructed capacity is 22 and size is 0
Capacity after 42 appends is 191 and size is 168
Capacity after clear() is 191 and size is 0
Capacity after shrink_to_fit() is 22 and size is 0
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::string shrink_to_fit() method

// Non const version only
constexpr void shrink_to_fit();

Requests the removal of unused capacity.
It is a non-binding request to reduce capacity() to size().
It depends on the implementation whether the request is fulfilled.

Invalidation

If reallocation takes place, all pointers, references, and iterators are invalidated.

Parameters

(none)

Return value

(none)

Complexity

Linear in the size of the container - O(size()).

Exceptions

(none)

Example

Main.cpp
#include <iostream>
#include <string>

int main()
{
std::string s;
std::cout << "Size of std::string is " << sizeof s << " bytes\n"
<< "Default-constructed capacity is " << s.capacity()
<< " and size is " << s.size() << '\n';
for (int i=0; i<42; i++)
s.append(" 42 ");
std::cout << "Capacity after 42 appends is " << s.capacity()
<< " and size is " << s.size() << '\n';
s.clear();
std::cout << "Capacity after clear() is " << s.capacity()
<< " and size is " << s.size() << '\n';
s.shrink_to_fit();
std::cout << "Capacity after shrink_to_fit() is " << s.capacity()
<< " and size is " << s.size() << '\n';
}
Possible output
GCC output:
Size of std::string is 32 bytes
Default-constructed capacity is 15 and size 0
Capacity after 42 appends is 240 and size 168
Capacity after clear() is 240 and size 0
Capacity after shrink_to_fit() is 15 and size 0

clang output (with -stdlib=libc++):
Size of std::string is 24 bytes
Default-constructed capacity is 22 and size is 0
Capacity after 42 appends is 191 and size is 168
Capacity after clear() is 191 and size is 0
Capacity after shrink_to_fit() is 22 and size is 0
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.