std::basic_string<CharT,Traits,Allocator>::pop_back
来自 cppreference.cn
< cpp | string | basic_string
void pop_back(); |
(C++20 起为 constexpr) | |
移除字符串的最后一个字符。
等价于 erase(end() - 1)。如果字符串为空,则行为未定义。
目录 |
[编辑] 参数
(无)
[编辑] 返回值
(无)
[编辑] 复杂度
常数时间。
[编辑] 异常
不抛出任何异常。
[编辑] 注意
在 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() |
已添加 |
[编辑] 参阅
在末尾添加字符 (公共成员函数) | |
移除字符 (公共成员函数) |