std::basic_string<CharT,Traits,Allocator>::push_back
From cppreference.com
< cpp | string | basic string
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
std::basic_string
Literals
Helper classes
Deduction guides (C++17)
(C++23)
(DR*)
(DR*)
(C++23)
basic_string::push_back
(DR*)
(C++23)
(C++23)
(C++20)
(C++20)
(C++23)
(C++20)(C++20)
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++14)
(C++11)
void push_back( CharT ch );
(constexpr 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 the operation would cause size()
to exceed max_size()
, throws std::length_error .
If an exception is thrown for any reason, this function has no effect (strong exception safety guarantee).
[edit] Example
Run this code
#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: 12 2) "Short string!", size: 13
[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
|
LWG 847 | C++98 | there was no exception safety guarantee | added strong exception safety guarantee |