Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | string‎ | basic string
Revision as of 13:55, 17 May 2023 by Space Mission (Talk | contribs)

 
 
 
std::basic_string
Member functions
Element access
Iterators
Capacity
Modifiers
basic_string::push_back
Search
Operations
Constants
Non-member functions
I/O
Comparison
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
Numeric conversions
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
Literals
Helper classes
Deduction guides (C++17)

 
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

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 <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

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

See also

removes the last character
(public member function) [edit]