232 Implement Queue using Stacks
⭐栈是以底层容器完成其所有的工作,对外提供统一的接口,底层容器是可插拔的(也就是说我们可以控制使用哪种容器来实现栈的功能)。
所以STL中栈往往不被归类为容器,而被归类为container adapter(容器适配器)。
栈的底层实现可以是vector,deque,list 都是可以的, 主要就是数组和链表的底层实现。
- STL 默认使用deque作为stack 和queue的容器
std::stack<int, std::vector<int> > third; // 使用vector为底层容器的栈
std::queue<int, std::list<int>> third; // 定义以list为底层容器的队列
Two Stacks: Instack and OutStack
Instack: Just Push to the top
OutStack: Pop and Top, everytime call the two methods and out stack is empty, we pop all the elements from in stack to out stack
225.Implement Stack using Queues
Every Time We try to pop, just keep pop until only one left