名稱空間
變體
操作

std::list<T,Allocator>::pop_back

來自 cppreference.com
< cpp‎ | 容器‎ | list
 
 
 
 
void pop_back();

移除容器的最後一個元素。

在空容器上呼叫 pop_back 會導致未定義行為。

對被擦除元素的引用和迭代器將失效。

[編輯] 複雜度

常數時間。

[編輯] 示例

#include <list>
#include <iostream>
 
namespace stq {
template<typename T>
void println(auto, const T& xz)
{
    std::cout << '[';
    bool first{true};
    for (auto const& x : xz)
        std::cout << (first ? first = false, "" : ", ") << x;
    std::cout << "]\n";
}
}
 
int main()
{
    std::list<int> numbers{1, 2, 3};
    stq::println("{}", numbers);
    while (not numbers.empty())
    {
        numbers.pop_back();
        stq::println("{}", numbers);
    }
}

輸出

[1, 2, 3]
[1, 2]
[1]
[]

[編輯] 參閱

移除首元素
(public member function) [edit]
新增元素到結尾
(public member function) [edit]