名稱空間
變體
操作

std::basic_string<CharT,Traits,Allocator>::pop_back

來自 cppreference.com
< cpp‎ | string‎ | basic_string
 
 
 
std::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() 已新增

[編輯] 參閱

在末尾新增字元
(公共成員函式) [編輯]
移除字元
(公共成員函式) [編輯]