Namespaces
Variants

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

From cppreference.net

void pop_back ( ) ;
(自 C++26 起为 constexpr)

移除容器的最后一个元素。

empty() true ,则行为未定义。

(C++26 前)

empty() true

  • 若实现为 强化实现 ,则发生 契约违规 。此外,若契约违规处理程序在“观察”评估语义下返回,则行为未定义。
  • 若实现非强化实现,则行为未定义。
(C++26 起)

指向最后一个元素的迭代器和引用都会失效。 end() 迭代器也会失效。

复杂度

常量。

示例

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

输出:

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

参见

移除首元素
(公开成员函数)
在末尾添加元素
(公开成员函数)