Queue(队列)

queue是一种先进先出的数据结构,它允许从最底端加入元素(push),最顶端取出元素(pop)。除此之外,不能用其他任何方式存取queue的元素,也就是说,queue不允许遍历行为。

与stack相似,queue也以deque为底部结构并封闭其底端的出口和前端的入口。

template<class T, class Sequence = deque<T>>
class stack{
    //__STL_NULL_TMPL_ARGS会展开为<>
    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 front() { return c.front(); }
    const_reference front() const { return c.front(); }
    reference back() { return c.back(); }
    const_reference back() const { return c.back(); }
    void push()(const value_type &x) { c.push_back(); }
    void pop() { c.pop_front(); }  
};

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;
}

queue由于结构的特殊性,不提供迭代器。

也可以使用list作为queue的底层容器。

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top