stack是一种先进后出的数据结构,只有一个出口,允许新增,移除,取得最顶端元素,但是
除了最顶端外,没有任何其他方法可以存取stack的其他元素.换言之,就是不允许有遍历行为
将元素推入stack的操作称为push,将元素推出stack的操作称为pop.
由于stack系一地步容器完成其所有工作,而具有这种修改某物接口,形成另一种风貌之性质者,称为adapter(配接器)
所以 STL stack往往不被归类为container(容器),而被归类为container adapter
//下面是stack的源码
template <class T,class Sequence=deque<T> >
class stack{
//以下的__STL_NULL_TMPL_AGRS会展开为< >
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:
//以下完全利用Sequence c的操作,完成stack的操作
bool empty() const{return c.empty();}
size_type size()const{return c.size();}
reference top(){return c.back();}