转载http://blog.csdn.net/thefutureisour/article/details/7751846
容器适配器
容器适配器其实就是一个接口转换装置,使得我们能用特定的方法去操作一些我们本来无法操作的东西。举一个例子,比如你的一个设备支持串口线,而你的电脑支持的是USB接口,这时候我们没必要重新买一个支持USB的设备,只需要一根串口转USB口的小玩意儿,让你的设备能够连接到USB接口上,这就是适配器。
那么C++中的容器适配器是做什么的呢?可以做一个类比,我们已有的容器(如vector、list、deque)就是设备,这个设备支持的操作很多,比如插入、删除、迭代器访问等等。而我们希望这个容器表现出来的是栈的样子:先进后出,入栈出栈等等,此时,我们没有必要重写一个新的数据结构,而只需把原有的容器重新封装一下,改变它的接口,就能把它当做栈来用了。
言归正传,理解了什么是适配器以后,其实问题就很简单了。C++中定义了3种容器适配器,它们让容器提供的接口变成了我们常用的的3种数据结构:栈(先进后出)队列(先进先出)和优先级队列(按照优先级(“<”号)排序,而不是按照到来的顺序排序)。
至于具体是怎么变的,我们可以先了解一个大概:默认情况下,栈和队列都是基于deque实现的,而优先级队列则是基于vector实现的。当然,我们也可以指定自己的实现方式。但是由于数据结构的关系,我们也不能胡乱指定。栈的特点是后进先出,所以它关联的基本容器可以是任意一种顺序容器,因为这些容器类型结构都可以提供栈的操作要求,即它们都提供了push_back、pop_back和back操作。 队列queue的特点是先进先出,适配器要求其关联的基础容器必须提供pop_front操作,因此其不能建立在vector容器上;对于优先级队列,由于它要求支持随机访问的功能,所以可以建立在vector或者deque上,不能建立在list上。

参见http://www.cplusplus.com/reference/queue/queue/
template <class T, class Container = deque<T> > class queue;

FIFO queue
queues are a type of container adaptor, specifically designed to operate in a FIFO context (first-in first-out), where elements are inserted into one end of the container and extracted from the other.
[队列(queue)是一种容器适配器,具有先进先出(FIFO)的结构,元素从一端插入从另一端提取]
queues are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed into the "back" of the specific container and popped from its "front".
[容器适配器是一个类,这个类使用一个特定的容器类对象作为内在容器,该内在容器提供了一系列的成员函数来读取它的元素]
The underlying container may be one of the standard container class template or some other specifically designed container class. This underlying container shall support at least the following operations:
[队列的内在容器可以是一个标准容器类模板或者是其他专门设计的容器类,但无论如何,内在容器都应该至少支持以下几种操作:]
empty
size
front
back
push_back
pop_front
The standard container classes deque and list fulfill these requirements. By default, if no container class is specified for a particular queue class instantiation, the standard container deque is used.
[标准容器类deque和list满足这些要求。默认的内在容器是双向队列deque]

/*
//construct queue
queue(const container_type& ctnr = container_type()) ctnr
Container object.
[ctnr是一个容器类对象]
container_type is the type of the underlying container type (defined as an alias of the second class template parameter, Container; see member types).
[ctnr的数据类型container_type是内在容器的数据类型,即第二个模板参数Container] A container adaptor keeps internally a container object as data. This container object is a copy of the ctnr argument passed to the constructor, if any, otherwise it is an empty container.
[queue会将一个容器对象作为数据,这个容器对象是传递到构造函数的参数ctnr,如果没有传递参数ctnr则为空容器]
*/ #include <iostream>
#include <deque>
#include <list>
#include <queue> int main()
{
std::deque<int> mydeck(, );
std::list<int> mylist(, ); std::queue<int> first; // empty queue with deque as underlying container
std::queue<int> second(mydeck); // queue initialized to copy of deque with deque as underlying container std::queue<int, std::list<int>> third; // empty queue with list as underlying container
std::queue<int, std::list<int>> fourth(mylist); // queue initialized to copy of list with list as underlying container std::cout << "size of first: " << first.size() << '\n';
std::cout << "size of second: " << second.size() << '\n';
std::cout << "size of third: " << third.size() << '\n';
std::cout << "size of fourth: " << fourth.size() << '\n'; system("pause");
return ;
}
/*
bool empty() const;
size_type size() const;
void push(const value_type& val);
void pop();
value_type& back();
value_type& front();
*/ #include <iostream>
#include <queue> int main()
{
std::queue<int> myque; for(int i=; i<; i++)
myque.push(i*); std::cout<<"myque contains: ";
while(!myque.empty())
{
std::cout<<myque.front()<<' ';
myque.pop();
} std::cout<<'\n'; system("pause");
return ;
}

容器适配器之queue的更多相关文章

  1. C++STL模板库适配器之queue队列

    目录 适配器之队列 一丶队列简介 二丶队列(queue)代码操作 1.常用方法 适配器之队列 一丶队列简介 队列是先进先出的数据结构. 在STL中使用 queue表示. 底层使用的是序列容器deque ...

  2. 容器适配器之priority_queue

    template <class T, class Container = vector<T>,                class Compare = less<type ...

  3. 容器适配器之stack

    参见http://www.cplusplus.com/reference/stack/stack/ template<class T, class Container = deque<T& ...

  4. C++STL模板库适配器之优先级队列

    目录 适配器之优先级队列 一丶优先级队列简介(priority_queue) 二丶优先级队列代码演示 1.优先级队列代码以及使用简介 适配器之优先级队列 一丶优先级队列简介(priority_queu ...

  5. 容器适配器(一):queue

    除了标准的顺序容器外,STL还提供了3种容器适配器,queue,priority_queue和stack 适配器是对顺序容器的包装,它的作用是简化接口. queue接口十分的简单,只有8个方法.再加上 ...

  6. C++STL模板库适配器之stack容器

    目录 适配器 一丶适配器简介 二丶栈(stack)用法 1.栈的常用方法 适配器 一丶适配器简介 Stl中的适配器,有栈 (stack) 队列 queue 根priority_queue 适配器都是包 ...

  7. STL容器适配器 stack, queue

    stack是一种后进先出(last in first out)的数据结构.它只有一个出口,如图所示.stack允许新增元素,删除元素,取得最顶端元素.但除了最顶端外,没有其他任何地方可以存储stack ...

  8. STL容器(Stack, Queue, List, Vector, Deque, Priority_Queue, Map, Pair, Set, Multiset, Multimap)

    一.Stack(栈) 这个没啥好说的,就是后进先出的一个容器. 基本操作有: stack<int>q; q.push(); //入栈 q.pop(); //出栈 q.top(); //返回 ...

  9. STL源码剖析——序列式容器#4 Stack & Queue

    Stack stack是一种先进后出(First In Last Out,FILO)的数据结构,它只有一个出口,元素的新增.删除.最顶端访问都在该出口进行,没有其他位置和方法可以存取stack的元素. ...

随机推荐

  1. poj3122 pie

    方法:二分. 题目意思:要过生日了,我请大家吃pie,然后人数一共是f+1(我自己).每个人的pie不能是拼接的,而且每个人的面积是一样的,这样就用二分枚举. 范围是0-最大的那块pie. 然后用每一 ...

  2. sql关联excel查询

    select * from 表名 where 字段名 in (SELECT excel列名 FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0','Data S ...

  3. Python之路【第五篇】:面向对象及相关

    Python之路[第五篇]:面向对象及相关   面向对象基础 基础内容介绍详见一下两篇博文: 面向对象初级篇 面向对象进阶篇 其他相关 一.isinstance(obj, cls) 检查是否obj是否 ...

  4. php实现文件上传的源码

    php实现文件上传的源码,更多php技术开发就去php教程网,http://php.662p.com <?php ##author :Androidyue ##sina @androidyue ...

  5. JavaScript计算日期间隔以及结果错误(少一天)的解决方法

    下面的代码是之前从网上某个地方COPY下来的,之前一直用着,前段时间DateDiff()方法突然出问题了,输入两个日期2015-10-01 和 2015-10-02之后,计算出来的日期是0!如果只有几 ...

  6. Lambda Grinding Miller From Zenith

    data = """ The Basic Things About Grinding Mill A grinding mill is a unit operation d ...

  7. 大话RAC介质恢复---只有备份文件的恢复

    场景:Oracle 10g RAC:数据文件.控制文件.联机日志.参数文件都使用ASM,归档到ASM.完整备份后,删除所有控制文件.联机日志.数据文件:最后利用备份进行不完全恢复. 1.模拟灾难场景( ...

  8. Oracle 10g 之自动收集统计信息

    从10g开始,Oracle在建库后就默认创建了一个名为GATHER_STATS_JOB的定时任务,用于自动收集CBO的统计信息.这个自动任务默认情况下在工作日晚上10:00-6:00和周末全天开启. ...

  9. 九度oj 1554 区间问题

    原题链接:http://ac.jobdu.com/problem.php?pid=1554 由数列的前缀和:$\begin{align*}\Large{} S_n &=\Large{}\sum ...

  10. spring使用JdbcDaoSupport中封装的JdbcTemplate进行query

    1.Dept package cn.hxex.springcore.jdbc; public class Dept { private Integer deptNo; private String d ...