c++ stl queue(FIFO,容器配接器)

1.queue

  queue是一种先进先出的数据结构,它有两个出口。queue允许新增元素、移除元素、从最底端加入元素、取得最顶端元素。但除了最底端可以加入、最顶端可以取出外,没有任何其他方法可以存取queue的其它元素。换言之,queue不允许有遍历行为。

  将元素推入queue的操作称为push,将元素推出queue的操作称为pop。



  以某种既有容器为底部结构,将其接口改变,使其符合“先进先出”的特性,形成一个queue,是很容易做到的。deque是双向开口的数据结构,若以deque为底部结构并封闭其底端的出口和前端的入口,便轻而易举地形成一个queue。因此,SGI STL便以deque作为缺省情况下的queue底部结构。

  由于queue系以底部容器完成其所有工作,而具有这种“修改某物接口,形成另一种风貌”之性质者,称为adapter,因此,STL stack往往不被归类为container(容器),而被归类为container adapter。

template <class T,class Sequence = deque<T> >
class queue
{
    friend bool operator == __STL_NULL_TMPL_ARGS (const queue& x,const queue& y);
    friend bool operator < __STL_NULL_TMPL_ARGS (const queue& x,const queue& y);
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(x); }
    void pop() { c.pop_front(); }
}
template <class T,class Sequence>
bool operator == (const queue<T,Sequence>& x,const queue<T,Sequence>& y)
{
    return x.c == y.c;
}
template <class T,class Sequence>
bool operator < (const queue<T,Sequence>& x,const queue<T,Sequence>& y)
{
    return x.c < y.c;
}

  queue所有元素的进出都必须符合“先进先出”的条件,只有queue顶端的元素,才有机会被外界取用。queue不提供遍历功能,也不提供迭代器。

2.函数成员

/// The default constructor. Creates an empty queue.
queue();
/// The copy constructor.
queue(const queue&);
/// The assignment operator.
queue& operator=(const queue&);
/// Returns true if the queue contains no elements, and false otherwise.
bool empty() const;
/// Returns the number of elements contained in the queue.
size_type size() const;
/// Returns a mutable reference to the element at the front of the queue, that is, the element least recently inserted.
value_type& front();
/// Returns a const reference to the element at the front of the queue, that is, the element least recently inserted.
const value_type& front() const;
/// Returns a mutable reference to the element at the back of the queue, that is, the element most recently inserted.
value_type& back();
/// Returns a const reference to the element at the back of the queue, that is, the element most recently inserted.
const value_type& back() const;
/// Inserts x at the back of the queue.
void push(const value_type& x);
/// Removes the element at the front of the queue.
void pop();
/// Compares two queues for equality.
bool operator==(const queue&, const queue&);
/// Lexicographical ordering of two queues.
bool operator<(const queue&, const queue&);

3.参考文献

  本文内容摘录于《STL源码剖析》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值