栈(Stack)是支持push和pop两种操作的数据结构。
{push { 放入
——>在栈的顶端 ——>一组数据的操作
}pop }取出
栈的特点:后进先出
注:c++的标准库中,stack::pop完成的仅仅是移除最顶端的数据,如果您要访问顶端数据,要用stack::top函数(这个函数通常也被称为peek)。
下面是stack的一个例子
#include<cstdio>
#include<stack>
using namespace std;
int main()
{
stack<int> s;//申明存储int类型数据的栈
s.push(1);//{}->{1}
s.push(2);//{1}->{1,2}
s.push(3);//{1,2}->{1,2,3}
printf("%d\n",s.top());//3
s.pop();//{1,2,3}->{1,2}
printf("%d\n",s.top());
s.pop();//{1,2}->{1}
printf("%d\n",s.top());
s.pop();{1}->{}
return 0;
}