|
|
|
void pop_back(); | (constexpr since C++20) | |
Removes the last character from the string.
Equivalent toerase(end()-1).
Ifempty() istrue, the behavior is undefined. | (until C++26) |
Ifempty() istrue:
| (since C++26) |
Contents |
Constant.
Throws nothing.
In libstdc++,pop_back()
isnot available in C++98 mode.
#include <cassert>#include <iomanip>#include <iostream>#include <string> int main(){std::string str("Short string!");std::cout<<"Before: "<<std::quoted(str)<<'\n';assert(str.size()==13); str.pop_back();std::cout<<"After: "<<std::quoted(str)<<'\n';assert(str.size()==12); str.clear();// str.pop_back(); // undefined behavior}
Output:
Before: "Short string!"After: "Short string"
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 534 | C++98 | std::basic_string did not have the member functionpop_back() | added |
appends a character to the end (public member function)[edit] | |
removes characters (public member function)[edit] |