std::vector<T,Allocator>::pop_back
来自 cppreference.cn
void pop_back(); |
(constexpr since C++20) | |
移除容器的最后一个元素。
在空容器上调用 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] []
[编辑] 参见
在末尾添加元素 (公共成员函数) |