如何让进栈和出栈后的顺序不变

以前我以为入栈和出栈后的顺序一定变反。那那么如何让顺序保持不变呢?如0,1,2,3进栈,那么如何让出栈后的顺序还是0,1,2,3?

#include<stack>
#include<iostream>
using namespace std;
void main()
{
 stack<int> s;
 s.push(0);
 cout<<s.top()<<endl;
 s.pop();
 s.push(1);
 cout<<s.top()<<endl;
 s.pop();
 s.push(2);
 cout<<s.top()<<endl;
 s.pop();
 s.push(3);
 cout<<s.top()<<endl;
 s.pop();

}

其实就是数字一进栈就出栈。

ps:如何灵活运用栈效果会很不错。

原文地址:https://www.cnblogs.com/zhmt/p/2171588.html