- 栈是一种先入后出的容器,STL中的stack默认采用deque实现,我们在声明栈时可以使用其它的序列式容器(如vector,list)实现栈。
#include <stack> //包含头文件
#include <iostream>
using namespace std;
stack<int> s1;
stack<int, vector<int>> s2; //用vector实现栈
s1.push(1); //push()入栈
s1.push(2);
s1.pop(); //pop()出栈
cout << s1.top(); //top()返回栈顶元素,但不移除
bool a = s1.empty(); //empty()测试栈是否为空
int b = s1.size(); //返回栈中元素个数
本文深入探讨了C++标准模板库(STL)中的栈(stack)容器,讲解了其基本操作,如push()、pop()、top()、empty()和size(),并展示了如何使用vector作为底层实现。
2941

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



