概念:stack是一种先进后出的数据结构,它只有一个出口,先进后出
栈中只有顶端的元素才可以被外界使用,因此栈不允许有遍历行为
入栈:push();
出栈:pop();
stack常用接口
- 构造函数
- stack<T> stk;
- stack(const stack& stk); //拷贝构造
- 赋值操作
- stack& operator=(const stack& stk);
- 数据存取
- push(elem); //栈顶添加
- pop(); //栈顶移除
- top(); //栈顶元素
- 大小操作
- empty();
- size();
#include <iostream>
#include <stack>
using namespace std;
void test()
{
stack<int> stk;
stk.push(10);
stk.push(20);
stk.push(30);
while (!stk.empty())
{
cout << stk.top() << endl;
stk.pop();
}
}
int main()
{
test();
system("pause");
return 0;
}