现在的位置: 首页 > 综合 > 正文

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

2013年03月08日 ⁄ 综合 ⁄ 共 335字 ⁄ 字号 评论关闭

以前我以为入栈和出栈后的顺序一定变反。那那么如何让顺序保持不变呢?如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:如何灵活运用栈效果会很不错。

抱歉!评论已关闭.