std::basic_string<CharT,Traits,Allocator>::pop_back
From cppreference.com
< cpp | string | basic string
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
std::basic_string
Literals
Helper classes
Deduction guides (C++17)
(C++23)
(DR*)
(DR*)
(C++23)
basic_string::pop_back
(DR*)
(C++23)
(C++23)
(C++20)
(C++20)
(C++23)
(C++20)(C++20)
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++14)
(C++11)
void pop_back();
(constexpr since C++20)
Removes the last character from the string.
Equivalent to erase(end() - 1).
If empty() is true, the behavior is undefined.
(until C++26)If empty() is true:
- If the implementation is hardened, a contract violation occurs. Moreover, if the contract-violation handler returns under "observe" evaluation semantic, the behavior is undefined.
- If the implementation is not hardened, the behavior is undefined.
[edit] Complexity
Constant.
[edit] Exceptions
Throws nothing.
[edit] Notes
In libstdc++, pop_back()
is not available in C++98 mode.
[edit] Example
Run this code
#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"
[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 534 | C++98 | std::basic_string did not have the member function pop_back()
|
added |