C++ Stack 库 - pop() 函数
描述
C++ 函数 std::stack::pop() 从堆栈中删除顶部元素并将堆栈大小减少一。 此函数在已删除元素上调用析构函数。
声明
以下是 std::stack::pop() 函数形式 std::stack 头的声明。
C++98
void pop();
参数
None
返回值
None
异常
取决于底层容器。
时间复杂度
常数,即 O(1)
示例
以下示例显示了 std::stack::pop() 函数的用法。
#include <iostream>
#include <stack>
using namespace std;
int main(void) {
stack<int> s;
for (int i = 0; i < 5; ++i)
s.emplace(i + 1);
while (!s.empty()) {
cout << s.top() << endl;
s.pop();
}
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 −
Stack contents are 5 4 3 2 1

