std::basic_string<CharT,Traits,Allocator>:: pop_back
| Classes | ||||
|
(C++17)
|
||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
void
pop_back
(
)
;
|
(自 C++20 起为 constexpr) | |
移除字符串的最后一个字符。
等价于 erase ( end ( ) - 1 ) 。
|
如果 empty() 为 true ,则行为未定义。 |
(C++26 前) |
|
如果 empty() 为 true : |
(C++26 起) |
目录 |
复杂度
常量。
异常
不抛出任何异常。
注释
在 libstdc++ 中,
pop_back()
方法在 C++98 模式下
不可用
。
示例
#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 }
输出:
Before: "Short string!" After: "Short string"
缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 问题报告 | 应用于 | 发布时的行为 | 正确行为 |
|---|---|---|---|
| LWG 534 | C++98 |
std::basic_string
没有成员函数
pop_back()
|
已添加 |
参见
|
向末尾追加字符
(公开成员函数) |
|
|
移除字符
(公开成员函数) |