码迷,mamicode.com
首页 > 其他好文 > 详细

stack、queue实现

时间:2015-01-16 14:37:03      阅读:310      评论:0      收藏:0      [点我收藏+]

标签:

//SGI STL以deque作为缺省情况下的stack底部结构,stack没有迭代器,不提供遍历功能
//queue的实现类似stack,也是以deque作为缺省底层结构
template <class T,class Sequence=deque<T>>
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;
}

 

stack、queue实现

标签:

原文地址:http://www.cnblogs.com/ljygoodgoodstudydaydayup/p/4228451.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!