名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | 容器‎ | vector
 
 
 
 
void pop_back();
(C++20 起為 constexpr)

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

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

指向最後一個元素的迭代器和引用會失效。end() 迭代器也會失效。

[編輯] 複雜度

常數時間。

[編輯] 示例

#include <vector>
#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::vector<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) [編輯]