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(): ...
随机推荐
- ESB数据采集思路
昨天接到一个任务,使用公司的ESB,调用别人的接口,把得到的数据存储到mysql数据库当中,这里简单记录解决思路,方便以后查看. 1.拿到一个网站的地址,使用火狐浏览器的firebug工具,查看其传递 ...
- 模板:abs用法
c语言书本上说,数学函数除了求整数的绝对值函数abs()之外<abs() 定义在stdlib.h中>,其余的函数都在头文件 math.h 中定义,包括对浮点数求绝对值的函数fabs().c ...
- IOS基础之设置APP的名字、设置图标、添加等待加载时的图片
1.我们的app一般默认的名字是我们的工程名字,所以我们一般会更改一个更加友好的名字,更改的方法如下图: 找到InfoPlist.strings文件,在里面添加语句CFBundleDisplayNam ...
- CSS3选择器:nth-child和:nth-of-type之间的差异
CSS3选择器:nth-child和:nth-of-type之间的差异 这篇文章发布于 2011年06月21日,星期二,23:04,归类于 css相关. 阅读 57546 次, 今日 143 次 by ...
- Flex-box 学习
.flex-cont{ /*定义为flexbox的“父元素”*/ display: -webkit-box; display: -webkit-flex; display: flex; /*子元素沿主 ...
- Android 与 Webservice 的快速保存
前面写了一篇android对sqllite的快速保存博客,今天我们来看看android与webservice的交互,相信很多有经验的人自然就想到了soap.但是如果在小型项目中,具有大材小用之嫌.实际 ...
- jQuery之$(document).ready()使用介绍
学习jQuery的第一件事是:如果你想要一个事件运行在你的页面上,你必须在$(document).ready()里调用这个事件 学习jQuery的第一件事是:如果你想要一个事件运行在你的页面上,你必须 ...
- 图片放大缩小(和ViewPager配合使用流畅显示)--第三方开源--PhotoView
图片的放大缩小实现效果是使用的github上的一个开源项目photoView实现的,下载地址:https://github.com/chrisbanes/PhotoView 下面看测试代码: acti ...
- RichTextBox 自动滚动到最后
RichTextBox.AppendText($"[{DateTime.Now.ToString("hh:mm:ss")}] {msg}\n"); RichTe ...
- java调用存储过程和函数
以对表test进行增,删,改,查进行说明:1.新建表test create table TEST ( TID NUMBER not null, TNAME VARCHAR2(32), TCODE VA ...