跳到主要内容

std::string pop_back() 方法

// Non const version only
void pop_back();

删除字符串的最后一个字符,如同通过以下方式:

erase(end()-1)
未定义行为

在空字符串上调用 pop_back()未定义行为

.

参数

(无)

返回值

(无)

复杂度

常数 - O(1)

异常

(无)

示例

Main.cpp
#include <cassert>
#include <string>
#include <iomanip>
#include <iostream>

int main()
{
std::string str("Short string!");
std::cout << "before=" << quoted(str) << '\n';
assert(str.size() == 13);

str.pop_back();
std::cout << " after=" << quoted(str) << '\n';
assert(str.size() == 12);

str.clear();
// str.pop_back(); // UB!
}
输出
before="Short string!"
after="Short string"
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”以查看此文档的所有更改。
悬停查看原始许可证。

std::string pop_back() 方法

// Non const version only
void pop_back();

删除字符串的最后一个字符,如同通过以下方式:

erase(end()-1)
未定义行为

在空字符串上调用 pop_back()未定义行为

.

参数

(无)

返回值

(无)

复杂度

常数 - O(1)

异常

(无)

示例

Main.cpp
#include <cassert>
#include <string>
#include <iomanip>
#include <iostream>

int main()
{
std::string str("Short string!");
std::cout << "before=" << quoted(str) << '\n';
assert(str.size() == 13);

str.pop_back();
std::cout << " after=" << quoted(str) << '\n';
assert(str.size() == 12);

str.clear();
// str.pop_back(); // UB!
}
输出
before="Short string!"
after="Short string"
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”以查看此文档的所有更改。
悬停查看原始许可证。