STL容器适配器:queue

1. 概念
和stack一样,queue也是一种容器适配器,其通过对既有容器进行包装,形成一种FIFO(First-in First-out)的数据结构。queue允许在一端进行插入,另一端进行访问,如下:

image

2. API
queue提供的API接口比较简单,如下:
(constructor)    Construct queue (public member function)
empty    Test whether container is empty (public member function)
size    Return size (public member function)
front    Access next element (public member function)
back    Access last element (public member function)
push    Insert element (public member function)
pop    Delete next element (public member function)

3. queue实现
和stack一样,queue也可以通过deque、list实现,有兴趣的同学可以参阅相关代码。queue所有元素的进出都必须符合FIFO的条件,不支持遍历,所以,queue不提供迭代器。

4. queue使用示例

代码如下:

#include
#include
using namespace std;
int main ()
{
  queue myints;
  cout << "0. size: " << (int) myints.size() << endl;
  for (int i=0; i<5; i++) myints.push(i);
  cout << "1. size: " << (int) myints.size() << endl;
  myints.pop();
  cout << "2. size: " << (int) myints.size() << endl;
  return 0;
}
 

输出结果:
0. size: 0
1. size: 5
2. size: 4


7. 结语
queue是一种容器适配器,通过对其他容器进行包装,使得元素只能以FIFO的形式访问,由于queue和deque特点比较相似,所以一般库使用deque作为queue的支持容器。

参考文献:
STL源码剖析

转载于:https://my.oschina.net/zipu888/blog/549634

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值