容器适配器之queue
转载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的更多相关文章
- C++STL模板库适配器之queue队列
目录 适配器之队列 一丶队列简介 二丶队列(queue)代码操作 1.常用方法 适配器之队列 一丶队列简介 队列是先进先出的数据结构. 在STL中使用 queue表示. 底层使用的是序列容器deque ...
- 容器适配器之priority_queue
template <class T, class Container = vector<T>, class Compare = less<type ...
- 容器适配器之stack
参见http://www.cplusplus.com/reference/stack/stack/ template<class T, class Container = deque<T& ...
- C++STL模板库适配器之优先级队列
目录 适配器之优先级队列 一丶优先级队列简介(priority_queue) 二丶优先级队列代码演示 1.优先级队列代码以及使用简介 适配器之优先级队列 一丶优先级队列简介(priority_queu ...
- 容器适配器(一):queue
除了标准的顺序容器外,STL还提供了3种容器适配器,queue,priority_queue和stack 适配器是对顺序容器的包装,它的作用是简化接口. queue接口十分的简单,只有8个方法.再加上 ...
- C++STL模板库适配器之stack容器
目录 适配器 一丶适配器简介 二丶栈(stack)用法 1.栈的常用方法 适配器 一丶适配器简介 Stl中的适配器,有栈 (stack) 队列 queue 根priority_queue 适配器都是包 ...
- STL容器适配器 stack, queue
stack是一种后进先出(last in first out)的数据结构.它只有一个出口,如图所示.stack允许新增元素,删除元素,取得最顶端元素.但除了最顶端外,没有其他任何地方可以存储stack ...
- STL容器(Stack, Queue, List, Vector, Deque, Priority_Queue, Map, Pair, Set, Multiset, Multimap)
一.Stack(栈) 这个没啥好说的,就是后进先出的一个容器. 基本操作有: stack<int>q; q.push(); //入栈 q.pop(); //出栈 q.top(); //返回 ...
- STL源码剖析——序列式容器#4 Stack & Queue
Stack stack是一种先进后出(First In Last Out,FILO)的数据结构,它只有一个出口,元素的新增.删除.最顶端访问都在该出口进行,没有其他位置和方法可以存取stack的元素. ...
随机推荐
- 【Qt】使用QProcess调用其它程序或脚本
大概试了一下,还是不错的,不过字符编码问题还不太好解决: 代码: #include "mainwindow.h" #include "ui_mainwindow.h&qu ...
- vsftp配置参数
转载:http://blog.chinaunix.net/uid-134240-id-172158.html listen_address=ip address 指定侦听IP listen_port= ...
- 基于IIS的HTTP、FTP文件服务器搭建与性能测试
鉴于CAPI中文件操作是非常重要的一环,为了提高性能,直接提供下载地址供客户端下载: 1.基于IIS的HTTP文件服务器.FTP文件服务器(为了减少因编码造成的性能问题,尽量不要在文件服务器上写代码) ...
- xdotool-linux下的按键精灵
这是我在ST写的用来自动打开机顶盒的脚本 #!/bin/bash init_stb() { xdotool type "telnet 10.80.117.$1" xdotool k ...
- 在javascript中如何取消事件冒泡
如果在javascript中只希望事件发生在它的目标而不是在它的父元素上,即取消它的冒泡事件的发生,该如何做?因为按照javascript发生事件的顺序,它由两个阶段:分别从根元素--父元素--目标元 ...
- linux下source命令的基本功能
source命令用法:source FileName作用:在当前bash环境下读取并执行FileName中的命令.注:该命令通常用命令“.”来替代.如:source .bash_rc 与 . .bas ...
- ASP.NET中的状态保持(转载)
状态是某一类型的数据在一定时期内保持活跃的信息.这里说的一定时期可以使整个应用程序的生命周期,可以使用户操作程序的时间,当然也可以是单个页面的生命周期等. 为了解决传统Web编程中固有的限制,ASP ...
- firefox 中碰到的一个小坑
情况描述: 在一个处于正常文档流的div中,里面有一部分文字,还有个有浮动的块, 上代码 HTML: <div class="container"> this is ...
- [C#] Extension Method 扩展方法
当我们引用第三方的DLL.或者Visual Studio自己的库的时候,或许会发现这样的一个情况,如果这个类型有一个XX的方法就好了.这时候我们可以用到扩展方法,是我们的代码更加灵活和高效. 这里我举 ...
- Java数字处理
给出一个不多于5位的正整数,要求如下: (1)求出该数是几位数. (2)分别打印出每一位数字. (3)按照逆序打印出各位数值. 按照以上要求,首先得用户从键盘输入一个不多于5位的正整数,可以用Syst ...