Elm*_*dov 5 c++ stack data-structures
我需要帮助返回没有pop()的堆栈的第二个元素?但我不知道我该怎么用.
我的代码:
stack<int> st;
st.push(10);
st.push(20);
st.top(); // return 20
Run Code Online (Sandbox Code Playgroud)
如何在不使用pop()的情况下使此函数返回10;
谢谢.
对不起我的英语.
我认为你试图模仿基于堆栈的机器?
这是使用std :: stack执行此操作的唯一方法:
stack<int> st;
st.push(10);
st.push(20);
int top = st.top(); // return 20
st.pop();
int second = st.top(); // return 10
st.push(top);
Run Code Online (Sandbox Code Playgroud)
如果你想要其他行为,你必须自己实现stack具有更多功能的行为.