注意
使用栈时需要头文件#include
区别
栈是先进后出,队列是先进先出。如下:
栈 队列
如1,2,3,4,5入栈,输出是5,4,3,2,1;1,2,3,4,5入队列,输出还是1,2,3,4,5
格式
栈:stack<类型(如int)>栈名
队列:queue<类型>队列名
常规操作
入栈/入队列:栈名/队列名.push(要入栈/队列的东西)
出栈:栈名.top()
出队列:队列名.front()
删除栈顶/队首:栈名/队列名.pop()
测试程序:
#includeusing namespace std; stack a; queue b; int main() { int n; int t; cin>>n; for(int i=1;i<=n;i++) { cin>>t; a.push(t); b.push(t); } cout<<"stack(栈): "; while(a.size()) { int x=a.top(); cout< 运行结果



