std::basic_string::resize
From cppreference.com
< cpp | string | basic string
| void resize( size_type count ); |
(1) | |
| void resize( size_type count, CharT ch ); |
(2) | |
Resizes the string to contain count characters.
If the current size is less than count, additional characters are appended.
If the current size is greater than count, the string is reduced to its first count elements.
The first version initializes new characters to CharT(), the second version initializes new characters to ch.
Contents |
[edit] Parameters
| count | - | new size of the string |
| ch | - | character to initialize the new characters with |
[edit] Return value
(none)
[edit] 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 (strong exception guarantee). (since C++11)
[edit] Example
Run this code
#include <iostream> #include <stdexcept> int main() { std::cout << "Basic functionality:\n"; const unsigned desired_length(8); std::string long_string( "Where is the end?" ); std::string short_string( "Ha" ); // Shorten std::cout << "Before: \"" << long_string << "\"\n"; long_string.resize( desired_length ); std::cout << "After: \"" << long_string << "\"\n"; // Lengthen std::cout << "Before: \"" << short_string << "\"\n"; short_string.resize( desired_length, 'a' ); std::cout << "After: \"" << short_string << "\"\n"; std::cout << "\nErrors:\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&) { std::cout << "1. bad alloc\n"; } try { // size is OK, no length_error // (may throw bad_alloc) s.resize(s.max_size(), 'x'); } catch (const std::bad_alloc& exc) { std::cout << "2. bad alloc\n"; } try { // size is BAD, throw length_error s.resize(s.max_size() + 1, 'x'); } catch (const std::length_error&) { std::cout << "3. length error\n"; } } }
Possible output:
Basic functionality: Before: "Where is the end?" After: "Where is" Before: "Ha" After: "Haaaaaaa" Errors: 1. bad alloc 2. bad alloc 3. length error
[edit] Complexity
Linear in the size of the string.
[edit] See also
| returns the number of characters (public member function) |