Difference between revisions of "cpp/string/basic string/push back"
From cppreference.com
< cpp | string | basic string
m (→Example: minor updates.) |
(Added LWG issue #7 DR (part 2).) |
||
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}} | {{dcl begin}} | ||
{{dcl rev multi | {{dcl rev multi | ||
− | | until1=c++20 | dcl1= | + | |until1=c++20|dcl1= |
void push_back( CharT ch ); | void push_back( CharT ch ); | ||
− | | dcl2= | + | |dcl2= |
constexpr void push_back( CharT ch ); | constexpr void push_back( CharT ch ); | ||
}} | }} | ||
Line 14: | Line 14: | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | ch | the character to append}} | + | {{par|ch|the character to append}} |
{{par end}} | {{par end}} | ||
Line 31: | Line 31: | ||
===Example=== | ===Example=== | ||
{{example | {{example | ||
− | | code= | + | |code= |
#include <cassert> | #include <cassert> | ||
#include <string> | #include <string> | ||
Line 47: | Line 47: | ||
assert(str.size() == 13); | assert(str.size() == 13); | ||
} | } | ||
− | + | |output= | |
before="Short string" | before="Short string" | ||
after="Short string!" | after="Short string!" | ||
}} | }} | ||
− | === | + | ===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|const CharT}}|after=(1) description added<br>(2) changed to {{c|CharT}}}} | ||
+ | {{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}} |
Revision as of 22:04, 3 August 2022
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
Run this code
#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!"
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
(DR*) |
removes the last character (public member function) |