std::basic_string<CharT,Traits,Allocator>::resize - cppreference.com

From cppreference.com

voidresize(size_typecount); (1)(constexpr since C++20)voidresize(size_typecount,CharTch); (2)(constexpr since C++20)Resizes the string to contain count characters.

If the current size is less than count, additional characters are 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.

Parameters

count - new size of the string ch - character to initialize the new characters with Exceptions

std::length_error

if count>max_size() is true. Any exceptions thrown by corresponding Allocator.

If an exception is thrown for any reason, this function has no effect (

strong exception safety guarantee

).

Example

Run this code

#include<iomanip>#include<iostream>#include<stdexcept>intmain(){constunsigneddesired_length{8};std::stringlong_string("Where is the end?");std::stringshort_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(charc:short_string)std::cout<<(c==char()?'@':c);std::cout<<"\"\n\n";std::cout<<"Errors:\n";std::strings;try{// size is OK, no length_error// (may throw bad_alloc)s.resize(s.max_size()-1,'x');}catch(conststd::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(conststd::bad_alloc&ex){std::cout<<"2. Exception: "<<ex.what()<<'\n';}try{// size is BAD, throw length_errors.resize(s.max_size()+1,'x');}catch(conststd::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.

DR Applied to Behavior as published Correct behavior

LWG 847

C++98 there was no exception safety guarantee added strong exception safety guarantee

LWG 2250

C++98 the behavior was undefined if
count>max_size() is truealways throws an exception in this case See also