Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/basic string/push back"

From cppreference.com
< cpp‎ | string‎ | basic string
(P0980R1)
m (Synopsis: {{ddcla}}; Defect reports: "(1)" => "1)" cuz former is mostly reserved to refer to overloads.)
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{cpp/string/basic_string/title | push_back}}
+
{{cpp/string/basic_string/title|push_back}}
 
{{cpp/string/basic_string/navbar}}
 
{{cpp/string/basic_string/navbar}}
{{dcl begin}}
+
{{ddcla|constexpr=c++20|
{{dcl rev multi
+
| until1=c++20 | dcl1=
+
 
void push_back( CharT ch );
 
void push_back( CharT ch );
| dcl2=
 
constexpr void push_back( CharT ch );
 
 
}}
 
}}
{{dcl end}}
 
  
Appends the given character {{tt|ch}} to the end of the string.
+
Appends the given character {{c|ch}} to the end of the string.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | ch | the character to append}}
+
{{par|ch|the character to append}}
 
{{par end}}  
 
{{par end}}  
  
Line 24: Line 19:
  
 
===Exceptions===
 
===Exceptions===
{{rev inl|since=c++11|
+
{{cpp/string/basic_string/length error note}}
If an exception is thrown for any reason, this function has no effect (strong exception guarantee).}}
+
  
If the operation would result in {{tt|size() > max_size()}}, throws {{lc|std::length_error}}.
+
{{cpp/strong exception safety guarantee}}
  
===See also===
+
===Example===
 +
{{example
 +
|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
 +
}}
 +
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=7|std=C++98|before=1) the description was missing in the C++ standard<br>2) the parameter type was {{c/core|const CharT}}|after=1) description added<br>2) changed to {{tt|CharT}}}}
 +
{{dr list item|wg=lwg|dr=847|std=C++98|before=there was no exception safety guarantee|after=added strong exception safety guarantee}}
 +
{{dr list end}}
 +
 +
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/string/basic_string/dsc pop_back}}
+
{{dsc inc|cpp/string/basic_string/dsc pop_back}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 14:04, 18 October 2024

 
 
 
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 );
(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 result in size() > 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

#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

[edit] See also

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