类似于queue,stack也是个简单的适配器。在实现上,stack与queue非常类似。底层可以使用同样的容器,比如默认都采用deque。因此,stack的源码和queue的非常类似。代码过于简单,都可以自解释了。下面是摘录出来的代码,可以顺便编译通过。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| #ifndef _STL_STACK_H #define _STL_STACK_H
namespace std { template <class T, class Sequence = deque<T> > class stack { friend bool operator==(const stack, const stack); friend bool operator<(const stack, const stack);
public: typedef typename Sequence::value_type value_type; typedef typename Sequence::size_type size_type; typedef typename Sequence::reference reference; typedef typename Sequence::const_reference const_reference; protected: Sequence c;
public: bool empty() const { return c.empty(); } size_type size() const { return c.size(); } reference top() { return c.back(); } const_reference top() const { return c.back(); }
void push(const value_type x) { c.push_back(x); } void pop() { c.pop_back(); } };
template <class T, class Sequence> bool operator==(const stack<T, Sequence> x, const stack<T, Sequence> y) { return x.c == y.c; }
template <class T, class Sequence> bool operator<(const stack<T, Sequence> x, const stack<T, Sequence> y) { return x.c < y.c; }
};
#endif
|
代码非常类似于queue,除了pop弹出的是末尾元素,top也是返回末尾元素之外。