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(): ...
随机推荐
- chromium浏览器开发系列第二篇:如何编译最新chromium源码
说一下为什么这么晚才发第二篇,上周和这周department的工作太多了,晚上都是十点半从公司出发,回家以后实在没有多余的精力去摸键盘了.所以请大家包涵! 上期回顾: chromium源码下载: 1. ...
- Exchange 2013 基本部署独立与非独立
Exchange 2013 基本部署独立与非独立 转载请注明原出处 From yang 先决条件 Active Directory需要准备的,安装Microsoft .NET Framework 4. ...
- PHP的接口(interface)
接口声明了函数和字段,但不会给出实现的细节 规则: 1.类全部为抽象方法(不需要声明abstract) 2.接口抽象方法必须是public 3.成员(字段)必须是常量 interface Comput ...
- 一些常用的字符串hash函数
unsigned int RSHash(const std::string& str) { unsigned int b = 378551; unsigned int a = 63689; u ...
- Git tricks: Unstaging files
NOTE: Following content is directly reprinted from http://andrewberls.com/blog/post/git-tricks-unsta ...
- Nginx +keepalived
Nginx +keepalived 配置高可用的Nginx 准备环境: 节点node17,node18 lftp 172.16.0.1:/pub/Sources/6.x86_64/nginx ...
- WP开发笔记——程序的退出方法
Windows Phone程序中,并没有之前的类似于“App.Exit()”之类的函数用来让你退出程序.这是怎么回事儿呢? 很简单,在Windows Phone 7中系统要求配备了硬件的“Back”键 ...
- Win7在CMD命令行中使用管理员权限运行命令
使用命令: runas /user:administrator 需要执行的命令 如下:
- 自己手写简约实用的Jquery tabs插件(基于bootstrap环境)
一直想改版网站首页的图书展示部分,以前的展示是使用BootStrap的传统的collapse,网页篇幅占用大,也不够美观,操作也相对来说比较麻烦.于是有了自己利用Jquery来做一个图书展示的tabs ...
- c# WinForm 编程总结
1.清空DataGridView /// <summary> /// 清空DataGridView /// </summary> /// <param name=&quo ...