From cppreference.com
voidpop_back();(constexpr since C++20)Removes the last character from the string.
Equivalent to erase(end()-1).
If
is true, the behavior is undefined.
(until C++26)If
is true:
If the implementation is
, a
occurs.
If the implementation is not hardened, the behavior is undefined.
(since C++26)Complexity
Constant.
Exceptions
Throws nothing.
Notes
In libstdc++, pop_back() is
in C++98 mode.
Example
Run this code
#include<cassert>#include<iomanip>#include<iostream>#include<string>intmain(){std::stringstr("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" Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++98
did not have the member function pop_back()added See also