std::basic_string<CharT,Traits,Allocator>::push_back

From cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
Member functions
Element access
Iterators
Capacity
Operations
basic_string::push_back
Search
Constants
Deduction guides (C++17)
Non-member functions
I/O
Comparison
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
Numeric conversion
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
Helper classes
 
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.

Parameters

ch - the character to append

Return value

(none)

Complexity

Amortized constant.

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.

Example

#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!"

See also

(C++11)
removes the last character
(public member function)