Namespaces
Variants

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

From cppreference.net

void pop_back ( ) ;
(constexpr C++26以降)

コンテナの最後の要素を削除します。

empty() true の場合、動作は未定義です。

(C++26まで)

empty() true の場合:

  • 実装が hardened されている場合、 contract violation が発生します。さらに、契約違反ハンドラが「observe」評価セマンティクスの下で戻った場合、動作は未定義です。
  • 実装がhardenedされていない場合、動作は未定義です。
(C++26以降)

削除された要素への参照とイテレータは無効化されます。

計算量

定数。

#include <list>
#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::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]
[]

関連項目

先頭要素を削除する
(公開メンバ関数)
末尾に要素を追加する
(公開メンバ関数)