std::basic_string<CharT,Traits,Allocator>::push_back
From cppreference.com
< cpp | string | basic string
| void push_back( CharT ch ); |
(until C++20) | |
| constexpr void push_back( CharT ch ); |
(since C++20) | |
Appends the given character ch to the end of the string.
Contents |
[edit] Parameters
| ch | - | the character to append |
[edit] Return value
(none)
[edit] Complexity
Amortized constant.
[edit] Exceptions
If an exception is thrown for any reason, this function has no effect (strong exception guarantee). (since C++11)
If the operation would result in size() > max_size(), throws std::length_error.
[edit] Example
Run this code
#include <cassert> #include <string> #include <iomanip> #include <iostream> int main() { std::string str{"Short string"}; std::cout << "before=" << std::quoted(str) << '\n'; assert(str.size() == 12); str.push_back('!'); std::cout << " after=" << quoted(str) << '\n'; assert(str.size() == 13); }
Output:
before="Short string" after="Short string!"
[edit] 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 7 | C++98 | (1) the description was missing in the C++ standard (2) the parameter type was const CharT |
(1) description added (2) changed to CharT |
[edit] See also
| (C++11) |
removes the last character (public member function) |