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

来自cppreference.com
< cpp‎ | container‎ | 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]
[]

参阅

移除首元素
(公开成员函数)
将元素添加到容器末尾
(公开成员函数)