首页 > 试题广场 >

上述程序的输出为( )

[单选题]
#include<bits/stdc++.h>
using namespace std;
int main(){
stack<int>st;
int pos = 1;
while(pos <= 3){
st.push(pos++);
}
cout<<st.top();
while(pos <= 5){
st.push(pos++);
}
while(!st.empty()){
cout<<st.top();
st.pop();
}
return 0;
}
上述程序的输出为(      )
  • 35421
  • 354321
  • 12453
  • 123453
top是取栈顶3,并没有将栈顶弹出,pop才是弹栈顶,所以出栈顺序为54321
编辑于 2019-09-11 21:58:03 回复(0)
push是压栈的操作,top是弹栈的操作,先如后出
发表于 2019-09-10 19:18:10 回复(0)