转载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. Hadoop的shell脚本分析

    你会发现hadoop-daemon.sh用于启动单独的本机节点 而hadoop-daemons.sh 会批量的ssh到别的机器启动 前记: 这些天一直学习hadoop,学习中也遇到了许多的问题,主要是 ...

  2. 图解Javascript上下文与作用域

    原文网址:http://blog.rainy.im/2015/07/04/scope-chain-and-prototype-chain-in-js/ 本文尝试阐述Javascript中的上下文与作用 ...

  3. 使用Spring开发第一个HelloWorld应用

    http://www.importnew.com/13246.html 让我们用Spring来写第一个应用程序吧. 完成这一章要求: 熟悉Java语言 设置好Spring的环境 熟悉简单的Eclips ...

  4. [原]NYOJ 括号匹配系列2,5

    本文出自:http://blog.csdn.net/svitter 括号匹配一:http://acm.nyist.net/JudgeOnline/problem.php?pid=2 括号匹配二:htt ...

  5. sublime搭建Java编译平台及编码问题

    Sublime自带Java编译功能,当时只能编译不能运行,我们做一下小小的修改就可以让sublime一步完成编译运行的功能,实现sublime搭建Java编译平台. 使用Ctrl + B 编译时,所编 ...

  6. javascript中for/in循环及使用技巧

    JavaScript 支持不同类型的循环: for - 循环代码块一定的次数 for/in - 循环遍历对象的属性 while - 当指定的条件为 true 时循环指定的代码块 do/while - ...

  7. wordpress代理设置

    打开wp-config.php,在页首加上以下语句: define('WP_PROXY_HOST', '192.168.84.101'); define('WP_PROXY_PORT', '8080' ...

  8. SqlServer存储过程学习笔记(增删改查)

    * IDENT_CURRENT 返回为任何会话和任何作用域中的特定表最后生成的标识值. CREATE PROCEDURE [dbo].[PR_NewsAffiche_AddNewsEntity] ( ...

  9. JQuery遍历指定id的div name值的几种方法

    JQuery遍历指定id的div name值的几种方法:方法一 $("#div1 :text").each(function () { var this_id = $(this). ...

  10. Oracle Database Cloud Services

    Oracle 开始也把数据库服务作为PaaS 服务,好吧 Oracle 叫做 DBaaS,数据库服务 https://cloud.oracle.com/database?tabID=138367891 ...