


using namespace std;
#include <iostream>
#include <stack>
/*
* stack容器
*/
void test() {
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
cout << s.size() << endl;
cout << endl;
while (!s.empty()) {
cout << s.top() << endl;
s.pop();
}
cout << endl;
cout << s.size() << endl;
}
int main() {
test();
return 0;
}
5
5
4
3
2
1
0
本文介绍了一个使用C++标准模板库(STL)中的栈容器(stack)的基本示例。示例展示了如何创建栈、向栈中添加元素(push)、获取栈顶元素(top)并移除(top)以及检查栈是否为空(empty)。通过此示例可以了解栈的基本操作及使用场景。
2615

被折叠的 条评论
为什么被折叠?



