首页 > 试题广场 >

#includeusing&nb...

[单选题]
#include <iostream>
#include <stack>
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是取出栈顶元素,不会删掉栈里边的元素。
pop是删除栈顶元素。
因此在第一次输出3的时候,只是取出,并没有删除,3仍然在栈中。
发表于 2020-11-27 18:42:36 回复(0)
错选a 是().top和().pop混淆不清
发表于 2020-08-31 19:39:56 回复(1)
stack是标准库中的一个容器适配器,是个类模板,使用的时候需要实例化,int是模板实参。stack<int>st声明了1个存储int型元素的栈,栈名是st。栈的特点是先进后出,123->3  12345 ->54321 ->354321

发表于 2020-08-27 09:48:56 回复(0)
top 用于查看栈顶元素,不弹出
发表于 2023-06-10 13:57:47 回复(0)
top只是读取并不删除,所以会一直在容器里面,pop才会删除。程序后面是不断读取,所以从头把容器里面元素从顶往下输出
发表于 2021-10-06 09:41:26 回复(0)