std::basic_string::resize(3)
β¨ 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:
- Initializes appended characters to
CharT()('\0'ifCharTischar). - 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 stringchβ character to initialize the new characters with
π¦ Return value π¦
(none) β literally nothing comes back, bestie. π«Ά
β οΈ Exceptions β οΈ
std::length_errorifcount > 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 toC++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
- 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