std::basic_...rink_to_fit(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ¦‹ 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 to C++98 β€” behavior as published: std::string lacked explicit shrink-to-fit operations β€” correct behavior: provided. slay for retroactive fixes πŸ’…

πŸ“ See also πŸ“

  • size β€” returns the number of characters
  • length β€” (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