pop
语法:
1 |
void pop(); |
pop()函数移除栈顶元素。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
源代码 /** * @brief Removes first element. * * This is a typical %stack operation. It shrinks the %stack * by one. The time complexity of the operation depends on the * underlying sequence. * * Note that no data is returned, and if the first element's * data is needed, it should be retrieved before pop() is * called. */ void pop() { __glibcxx_requires_nonempty(); c.pop_back(); } |
测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// 梁笔记 // https://zouzhongliang.com #include <iostream> #include <stack> using namespace std; int main() { stack<int> Si; Si.push(1); Si.push(2); Si.push(3); Si.push(4); Si.push(4); Si.push(5); while(!Si.empty()){ cout<<Si.top()<<endl; Si.pop(); //移除栈顶元素 } } |
测试结果
1 2 3 4 5 6 |
5 4 4 3 2 1 |