std::basic_...rink_to_fit(3)
π¦ std::basic_string::shrink_to_fit π¦
okay so this page is from the C++ Standard Libary manual, section 3 β the page title is std::basic_...rink_to_fit(3) and we're about to make your string's excess capacity pack its bags. bestie, it's giving "cleanup era" β¨
π NAME π
std::basic_string::shrink_to_fit β yep, that's the name, and the page repeats it as std::basic_string::shrink_to_fit. iconic redundancy, no notes. π
πΈ Synopsis πΈ
void shrink_to_fit(); (constexpr since C++20)
Requests the removal of unused capacity.
It is a non-binding request to reduce capacity() to size(). Whether the request is actually fulfilled depends on the implementation.
If (and only if) reallocation takes place, all pointers, references, and iterators are invalidated. so like, don't be holding onto those, bestie π
π Parameters π
(none) β not a single parameter, she's low maintenance.
π«Ά Return value π«Ά
(none) β returns absolutely nothing, and that's the vibe.
π₯ Complexity π₯
(unspecified)β until C++17- Linear in the size of the string. β since C++17
π§ Notes π§
In libstdc++, shrink_to_fit() is not available in C++98 mode. periodt.
β¨ Example β¨
// Run this code
#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
(note the GCC line really says Default-constructed capacity is 15 and size 0 β we keep it exactly, no cap π§’)
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 755β applied toC++98β behavior as published:std::stringlacked explicit shrink-to-fit operations β correct behavior: provided. slay for retroactive fixes π
π See also π
sizeβ returns the number of characterslengthβ (public member function)capacityβ returns the number of characters that can be held in currently allocated storage (public member function)resizeβ changes the number of characters stored (public member function)
okay that's the whole page, bestie! big kiss from http://cppreference.com, version/date 2024.06.10, and the C++ Standard Libary manual β std::basic_...rink_to_fit(3) out. ππ«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms