1、stack概述
stack是一种先进后出(FILO)的数据结构,stack允许新增元素、移除元素、取最顶端元素,但不允许取除最顶端外其他元素所以不允许有遍历行为。stack的底层实现机制是deque,deque是双向开口的数据结构只要封闭其头端开口便可以轻而易举实现stack。stack是以deque为底部容器完成所有工作,所以stack是配接器(adapter)。stack不提供遍历功能,也不提供迭代器。
template <class T, class Sequence = deque<T> >
template <class T, class Sequence>
class stack {
friend bool operator== __STL_NULL_TMPL_ARGS (const stack&, const stack&);
friend bool operator< __STL_NULL_TMPL_ARGS (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;
}