【模板】栈
输出描述:
如果操作为push,则不输出任何东西。
如果为另外两种,若栈为空,则输出 "error“
否则按对应操作输出。
示例1
输入:
6
push 1
pop
top
push 2
push 3
pop
输出:
1
error
3
#include<bits/stdc++.h>
using namespace std;
//栈
int main(){
stack <int> num;
int n,x;
string str;
cin>>n;
while(n--){
cin>>str;
if(str=="push"){
cin>>x;
num.push(x);
}
else if(str=="pop"){
if(!num.empty()){
cout<<num.top()<<endl;
num.pop();
}
else{
cout<<"error"<<endl;
}
}
else if(str=="top"){
if(!num.empty()){
cout<<num.top()<<endl;
}
else{
cout<<"error"<<endl;
}
}
}
return 0;
}