首页 > 试题广场 >

上述程序的输出为( )

[单选题]
#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();
弹出栈底元素 button();  都是弹出副本。
发表于 2019-09-16 20:26:03 回复(0)
堆栈是先进后出,push()是进栈,top()是出栈。 注意top()是获取栈顶,即最后进入的元素的值, button()是获取栈底的值,即最先进入栈的元素的值
发表于 2019-08-15 14:49:45 回复(0)