STL中的单向队列queue
转载自:http://blog.csdn.net/morewindows/article/details/6950917
stl中的queue指单向队列,使用时,包含头文件<queue>。
关键要会用queue,实际上就是掌握该类的各种操作,如下:
常用函数push(e),pop(),front(),back(),size(),empty(),与栈的常用函数较为相似。
在STL中,单向队列是以别的容器作为底层数据结构,再改变接口使之符合单向队列的特性。下面就给出单向队列的函数列表和VS2008中单向队列的源代码。

VS2008中queue单向队列的源代码
友情提示:初次阅读时请注意其实现思想,不要在细节上浪费过多的时间。
template<class _Ty, class _Container = deque<_Ty> >
class queue
{ // FIFO queue implemented with a container
public:
typedef _Container container_type;
typedef typename _Container::value_type value_type;
typedef typename _Container::size_type size_type;
typedef typename _Container::reference reference;
typedef typename _Container::const_reference const_reference; queue() : c()
{ // construct with empty container
} explicit queue(const _Container& _Cont) : c(_Cont)
{ // construct by copying specified container
} bool empty() const
{ // test if queue is empty
return (c.empty());
} size_type size() const
{ // return length of queue
return (c.size());
} reference front()
{ // return first element of mutable queue
return (c.front());
} const_reference front() const
{ // return first element of nonmutable queue
return (c.front());
} reference back()
{ // return last element of mutable queue
return (c.back());
} const_reference back() const
{ // return last element of nonmutable queue
return (c.back());
} void push(const value_type& _Val)
{ // insert element at beginning
c.push_back(_Val);
} void pop()
{ // erase element at end
c.pop_front();
} const _Container& _Get_container() const
{ // get reference to container
return (c);
} protected:
_Container c; // the underlying container
};</span>
由以上可以看出,单向队列queue封装了别的底层数据结构(默认为deque),并改动接口以实现自身特性。
下面给出单向队列的使用范例:
//单向队列 queue支持 empty() size() front() back() push() pop() #include <queue>
#include <vector>
#include <list>
#include <cstdio>
using namespace std; int main()
{
//可以使用list作为单向队列的容器,默认是使用deque的。
queue<int, list<int>> a;
queue<int> b;
int i; //压入数据
for (i = ; i < ; i++)
{
a.push(i);
b.push(i);
} //单向队列的大小
printf("%d %d\n", a.size(), b.size()); //队列头和队列尾
printf("%d %d\n", a.front(), a.back());
printf("%d %d\n", b.front(), b.back()); //取单向队列项数据并将数据移出单向队列
while (!a.empty())
{
printf("%d ", a.front());
a.pop();
}
putchar('\n'); while (!b.empty())
{
printf("%d ", b.front());
b.pop();
}
putchar('\n');
return ;
}
由以上可知,stl中的单向队列queue以deque(双向队列)为默认的底层数据结构,但queue的实现也可以用list(单链表)作为底层数据结构。
PS:不可以用vector作为底层,原因在于:vector不支持pop_front()。
总之,stl中的单向队列queue:
1、包含头文件<queue>;
2、6个常用操作:size():queue中元素个数
empty():判空
front():取队首元素
back():取队尾元素
push(e):入队尾
pop():删队首
STL中的单向队列queue的更多相关文章
- java-Enumeration,单向队列Queue及双向队列Deque等容器简单使用
1.Enumeration容器使用: package com.etc; import java.util.Enumeration; import java.util.Vector; /* Enumer ...
- Python 单向队列Queue模块详解
Python 单向队列Queue模块详解 单向队列Queue,先进先出 '''A multi-producer, multi-consumer queue.''' try: import thread ...
- STL中的优先级队列priority_queue
priority_queue(queue类似)完全以底部容器为根据,再加上二叉堆(大根堆或者小根堆)的实现原理,所以其实现非常简单,缺省情况下priority_queue以vector作为底部容器.另 ...
- (hdu step 8.1.1)ACboy needs your help again!(STL中栈和队列的基本使用)
题目: ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 线性表:实现单链表和子类栈(Stack)及单向队列(Queue) [C++]
刚刚开始学习c++.之前c的内容掌握的也不多,基本只是一本概论课的程度,以前使用c的struct写过的链表.用python写过简单的数据结构,就试着把两者用c++写出来,也是对c++的class,以及 ...
- Python 双向队列Deque、单向队列Queue 模块使用详解
Python 双向队列Deque 模块使用详解 创建双向队列Deque序列 双向队列Deque提供了类似list的操作方法: #!/usr/bin/python3 import collections ...
- STL中的优先级队列(priority_queue)的自己实现priqueue
这篇文章主要介绍堆(最大堆和最小堆),以及一些系统对一些任务,比如线程,进程做调度的时候,所采用的优先级队列. 主要思想就是,做一个最大堆(任务的权重最大的在顶端),把顶端的任务取出,重新做一个堆,处 ...
- 用数组模拟STL中的srack(栈)和queue(队列)
我们在理解stack和queue的基础上可以用数组来代替这两个容器,因为STL中的stack和queue有可能会导致程序运行起来非常的慢,爆TLE,所以我们使用数组来模拟他们,不仅可以更快,还可以让代 ...
- STL队列 之FIFO队列(queue)、优先队列(priority_queue)、双端队列(deque)
1.FIFO队列 std::queue就是普通意思上的FIFO队列在STL中的模版. 1.1主要的方法有: (1)T front():访问队列的对头元素,并不删除对头元素 (2)T back(): ...
随机推荐
- Android Bitmap那些事之如何优化内存
前言:”安得广厦千万间,大庇天下寒士俱欢颜“——杜甫.在帝都住的朋友们都可能会遇到租房子困难的问题(土豪请无视),找房子真是力气活,还耗费时间,占用我宝贵的写博客时间,没办法,谁让咱没钱还想住的好点, ...
- 在ASP.NET中发送电子邮件的实例教程
首先.导入命名空间: 代码如下 复制代码 using System.Net.Mail; 定义发送电子邮件的方法[网上很多不同的,可以对比着看一下,WinForm的也适用]: 代码如下 复制代码 /// ...
- CCNA长语
思科认证网络支持工程师(Cisco Certified Network Associate_CCNA) 专业英文词汇大全 10BaseT-----原始IEEE802.3标准的一部分,1OBaseT是1 ...
- WCF之可靠性
可靠性会话: 端到端(多个点到点系统组成)的可靠性,基于消息,基于WS-*,可以跨平台. 在信道层创建可靠性会话,由两端的缓冲区进行可靠性管理(对消息进行排序后才发给服务器端,接收到消息后回发ACK. ...
- trident 序列号问题
在使用Storm的trident做流计算开发时,遇到一个诡异的问题: 我继承IPartitionedTridentSpout或者IOpaquePartitionedTridentSpout接口做事务型 ...
- Install GDAL in OpenSUSE 12.3 Linux
Runtime Enviroment:Open SUSE Linux *i385 Notice:if any command disavliable ,you can copy the paramet ...
- unity 3消 游戏
3消游戏跟着智能手机流行到现在已经有很长一段时间,unity实现的3消 https://github.com/textcube/match3action 截图如下: 在阅读源码的时候不难发现,Game ...
- unity 多线程
对于客户端来说,好的用户体验,需要保持一个快速响应的用户界面.于是便要求:网络请求.io操作等 开销比较大的操作必须在后台线程进行,从而避免主线程的ui卡顿.(注:协程也是主线程的一部分,进行大量的i ...
- Windows Phone 7 中拷贝文件到独立存储
private void CopyToIsolatedStorage(){ using (IsolatedStorageFile storage = IsolatedStorageFile.Ge ...
- daxuez.com
大学z,一个还没想好的名字和项目 初期定位:服务于武汉各大高校的大学学生群体 服务项目:兼职.旅游.培训.租车.班服订做.票务