std::basic_string::resize(3)

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

✨ NAME ✨

okay bestie, welcome to the std::basic_string::resize(3) page from the C++ Standard Libary manual (yes, that spelling is in the source, don't come for me πŸ™ˆ). This is std::basic_string::resize β€” std::basic_string::resize. That's the NAME line, and it's literally the same name twice. iconic behavior, honestly. πŸ’…


🌸 Synopsis 🌸

Here's the exact overload lineup, no cap:

void resize( size_type count );           (1) (constexpr since C++20)
void resize( size_type count, CharT ch ); (2) (constexpr since C++20)

It resizes the string so it contains count characters.

If the current size is less than count, extra characters get appended:

  1. Initializes appended characters to CharT() ('\0' if CharT is char).
  2. Initializes appended characters to ch.

If the current size is greater than count, the string is reduced to its first count elements. Chopped, periodt. βœ‚οΈ


πŸŽ€ Parameters πŸŽ€

  • count β€” new size of the string
  • ch β€” character to initialize the new characters with

πŸ¦‹ Return value πŸ¦‹

(none) β€” literally nothing comes back, bestie. 🫢


⚠️ Exceptions ⚠️

  • std::length_error if count > max_size().
  • Any exceptions thrown by corresponding Allocator.
If an exception is thrown for any reason, this function has no effect β€” that's the strong exception safety guarantee. All-or-nothing icon behavior, fr fr. πŸ’€

πŸ’– Example πŸ’–

// Run this code

#include <iomanip>
#include <iostream>
#include <stdexcept>

int main()
{
    const unsigned desired_length{8};
    std::string long_string("Where is the end?");
    std::string short_string("H");

    std::cout << "Basic functionality:\n"
              << "Shorten:\n"
              << "1. Before: " << std::quoted(long_string) << '\n';
    long_string.resize(desired_length);
    std::cout << "2. After:  " << std::quoted(long_string) << '\n';

    std::cout << "Lengthen with a given value 'a':\n"
              << "3. Before: " << std::quoted(short_string) << '\n';
    short_string.resize(desired_length, 'a');
    std::cout << "4. After:  " << std::quoted(short_string) << '\n';

    std::cout << "Lengthen with char() == " << static_cast<int>(char()) << '\n'
              << "5. Before: " << std::quoted(short_string) << '\n';
    short_string.resize(desired_length + 3);
    std::cout << "6. After:  \"";
    for (char c : short_string)
        std::cout << (c == char() ? '@' : c);
    std::cout << "\"\n\n";

    std::cout << "Errors:\n";
    std::string s;

    try
    {
        // size is OK, no length_error
        // (may throw bad_alloc)
        s.resize(s.max_size() - 1, 'x');
    }
    catch (const std::bad_alloc& ex)
    {
        std::cout << "1. Exception: " << ex.what() << '\n';
    }

    try
    {
        // size is OK, no length_error
        // (may throw bad_alloc)
        s.resize(s.max_size(), 'x');
    }
    catch (const std::bad_alloc& ex)
    {
        std::cout << "2. Exception: " << ex.what() << '\n';
    }

    try
    {
        // size is BAD, throw length_error
        s.resize(s.max_size() + 1, 'x');
    }
    catch (const std::length_error& ex)
    {
        std::cout << "3. Length error: " << ex.what() << '\n';
    }
}

🌈 Possible output 🌈

Basic functionality:
Shorten:
1. Before: "Where is the end?"
2. After:  "Where is"
Lengthen with a given value 'a':
3. Before: "H"
4. After:  "Haaaaaaa"
Lengthen with char() == 0
5. Before: "Haaaaaaa"
6. After:  "Haaaaaaa@@@"

Errors:
1. Exception: std::bad_alloc
2. Exception: std::bad_alloc
3. Length error: basic_string::_M_replace_aux

🩹 Defect reports 🩹

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • LWG 847 β€” applied to C++98; behavior as published: there was no exception safety guarantee; correct behavior: added strong exception safety guarantee.

πŸ‘‘ See also πŸ‘‘

  • size β€” returns the number of characters (public member function)
  • length β€” (public member function)
  • reserve β€” reserves storage (public member function)
  • shrink_to_fit β€” reduces memory usage by freeing unused memory; (DR*) (public member function)

source/version: http://cppreference.com Β· dated 2024.06.10 Β· page std::basic_string::resize(3) β€” byeee, ily, go resize something cute πŸ’–πŸŽ€

πŸ“¦ where this page comes from