#include <iostream> #include <algorithm> #include "string.h" #include "stdio.h" #include <vector> #include <deque> #include <stack> #include<map> #include<utility> #include "math.h" using namespace std; class Solution { public: void push(int node) { stack1.push(node); } int pop() { int res; if(stack2.empty()&&!stack1.empty()) { while(!stack1.empty()) { int p = stack1.top(); stack2.push(p); stack1.pop(); } } if(stack2.empty()) { return -1; } res = stack2.top(); stack2.pop(); return res; } private: stack<int> stack1; stack<int> stack2; }; int main() { vector<int> arr; arr.push_back(1); arr.push_back(2); arr.push_back(3); Solution solution; for(int i=0;i<arr.size();i++) { solution.push(arr[i]); } cout<<solution.pop()<<endl; solution.push(4); cout<<solution.pop()<<endl; cout<<solution.pop()<<endl; cout<<solution.pop()<<endl; }
原文:http://www.cnblogs.com/omelet/p/6648391.html