|
|
|
void push_back( CharT ch); | (constexpr since C++20) | |
Appends the given characterch to the end of the string.
Contents |
ch | - | the character to append |
(none)
Amortized constant.
If the operation would causesize()
to exceedmax_size()
, throwsstd::length_error.
If an exception is thrown for any reason, this function has no effect (strong exception safety guarantee).
#include <iomanip>#include <iostream>#include <string> int main(){std::string str{"Short string"};std::cout<<"1) "<<std::quoted(str)<<", size: "<< str.size()<<'\n'; str.push_back('!');std::cout<<"2) "<<std::quoted(str)<<", size: "<< str.size()<<'\n';}
Output:
1) "Short string", size: 122) "Short string!", size: 13
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 7 | C++98 | 1) the description was missing in the C++ standard 2) the parameter type wasconst CharT | 1) description added 2) changed to CharT |
LWG 847 | C++98 | there was no exception safety guarantee | added strong exception safety guarantee |
(DR*) | removes the last character (public member function)[edit] |