C++ STL模板法
2026-11-16 09:42:00
发布于:湖北
0阅读
0回复
0点赞
我们这里可以不用数组来模拟,用STL里的stack就可以了,简洁优昂
#include<bits/stdc++.h>
using namespace std;
#define en endl
int main(){
stack<int>st;
int n;
cin>>n;
for(int i=0;i<n;i++){
string a;
int k;
cin>>a;
if(a=="empty"){
if(st.empty()){
cout<<"yes"<<en;
}else{
cout<<"no"<<en;
}
}else if(a=="push"){
cin>>k;
st.push(k);
}
else if(a=="size"){
cout<<"size = "<<st.size()<<en;
}
else if(a=="top"){
if(!st.empty()){
cout<<"top = "<<st.top()<<en;
}
else{cout<<"top fail"<<en;}
}
else if(a=="pop"){
if(!st.empty()){
cout<<"pop "<<st.top()<<en;
st.pop();
}else{
cout<<"pop fail"<<en;
}
}
}
return 0;
}
有用的话记得点赞
这里空空如也








有帮助,赞一个