std::string push_back() method
- od C++20
- do C++20
constexpr void push_back( CharT ch );
// Non const version only
void push_back( CharT ch );
Appends the given character ch to the end of the string.
Parameters
ch
- the character to append
Return value
(none)
Complexity
Amortized constant - O(1).
Exceptions
If the operation would result in size() > max_size()
, throws std::length_error
.
Example
Main.cpp
#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!"
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.
Hover to see the original license.