容器适配器之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的元素. ...
随机推荐
- 条款22 template method 模式
template method 模式,模板方法模式 其实他和C++模板没有关系. 前者是提供的为派生类设计者提供清晰指示的一种方法,这个事实表示"如何去实现基类所规定的契约" 基类 ...
- Mysql的ssl主从复制+半同步主从复制
Mysql的ssl主从复制+半同步主从复制 准备工作 1.主从服务器时间同步 [root@localhost ~]# crontab -e */30 * * * * /usr/sbin/ntpdate ...
- Windows2003屏蔽IP
1.打开本地安全策略 2.创建新的IP策略 去掉勾选向导 我们编辑 直接右键指派 指派可以看出来生效...网络已经不通了
- 2014第五届蓝桥杯试题C/C++程序设计B组——切面条
题目描述:标题:切面条 一根高筋拉面,中间切一刀,可以得到2根面条. 如果先对折1次,中间切一刀,可以得到3根面条. 如果连续对折2次,中间切一刀,可以得到5根面条. 那么,连续对折10次,中间切一刀 ...
- css中li、a、span行内强制不换行
li.a.span行内强制不换行:white-space:nowrap; 没有之前的效果 加上white-space:nowrap;后
- 【php学习之路】php基础语法
一.什么是php? PHP即PHP: Hypertext Preprocessor(超文本处理器),是一种服务器端脚本语言,适用于创建web站点.开源免费 二.php能做什么? ...
- Zend Studio下调试PHP的一点注意事项
Zend Studio默认php文件的存放路径是你配置的服务器的路径,比如你配置的服务器是localhost,那么,你在zend下建立的文件均是相对于localhost而言的,比如你新建一个php工程 ...
- python Django 学习笔记(三)—— 模版的使用
模版基本介绍 模板是一个文本,用于分离文档的表现形式和内容. 模板定义了占位符以及各种用于规范文档该如何显示的各部分基本逻辑(模板标签). 模板通常用于产生HTML,但是Django的模板也能产生任何 ...
- 如何解决android studio 运行时中文乱码的问题
相信很多朋友都会遇到android studio 在MAC OS中运行的时候中文乱码.而在代码编辑的时候正常.经过几天的不断寻找解决办法,终于解决了 比如: Toast.makeText(MainAc ...
- 【C#】使用C#将类序列化为XML
直接上代码: public static class XmlSerializer { public static void SaveToXml(string filePath, object sour ...