优先队列priority_queue的简单应用
优先队列
引入
优先队列是一种特殊以及强大的队列。
那么优先队列是什么呢?
说白了,就是一种功能强大的队列。
它的功能强大在哪里呢?
四个字:自动排序。
优先队列的头文件&&声明
头文件:
- #include<queue>
- using namespace std;
其次,一个优先队列声明的基本格式是:
priority_queue<结构类型> 队列名;
- priority_queue<int> i;
- priority_queue<double> i;
不过这是新手级别的,下面的是大佬级别的:
- priority_queue <node> q;
- //node是一个结构体
- //结构体里重载了‘<’小于符号
- priority_queue <int,vector<int>,greater<int> > q;
- //不需要#include<vector>头文件
- //注意后面两个“>”不要写在一起,“>>”是右移运算符
- priority_queue <int,vector<int>,less<int> >q;
优先队列的基本操作
与队列差不多:
- q.size();//返回q里元素个数
- q.empty();//返回q是否为空,空则返回1,否则返回0
- q.push(k);//在q的末尾插入k
- q.pop();//删掉q的第一个元素
- q.top();//返回q的第一个元素
- q.back();//返回q的末尾元素
优先队列的特性
默认的优先队列(非结构体结构)
priority_queue <int> q;
- #include<cstdio>
- #include<queue>
- using namespace std;
- priority_queue <int> q;
- int main()
- {
- q.push(),q.push(),q.push(),q.push(),q.push();
- while(!q.empty())
- printf("%d ",q.top()),q.pop();
- }
程序大意就是在这个优先队列里依次插入10、8、12、14、6,再输出。
结果是什么呢? 14 12 10 8 6
也就是说,它是按从大到小排序的!
默认的优先队列(结构体,重载小于)
- struct node
- {
- int x,y;
- bool operator < (const node & a) const
- {
- return x<a.x;
- }
- };
这个node结构体有两个成员,x和y,它的小于规则是x小者小。
再来看看验证程序:
- #include<cstdio>
- #include<queue>
- using namespace std;
- struct node
- {
- int x,y;
- bool operator < (const node & a) const
- {
- return x<a.x;
- }
- }k;
- priority_queue <node> q;
- int main()
- {
- k.x=,k.y=; q.push(k);
- k.x=,k.y=; q.push(k);
- k.x=,k.y=; q.push(k);
- k.x=,k.y=; q.push(k);
- k.x=,k.y=; q.push(k);
- while(!q.empty())
- {
- node m=q.top(); q.pop();
- printf("(%d,%d) ",m.x,m.y);
- }
- }
程序大意就是插入(10,100),(12,60),(14,40),(6,20),(8,20)这五个node。
再来看看它的输出: (14,40) (12,60) (10,100) (8,20) (6,80)
它也是按照重载后的小于规则,从大到小排序的。
less和greater优先队列
- priority_queue <int,vector<int>,less<int> > p;
- priority_queue <int,vector<int>,greater<int> > q;
less是从大到小,greater是从小到大。
同时还可以自己定义规则 cmp;
2、自定义优先级:
3、结构体声明方式:
- /*优先队列的基本使用 2017/8/1 xzxl*/
- #include<stdio.h>
- #include<functional>
- #include<queue>
- #include<vector>
- using namespace std;
- //定义结构,使用运算符重载,自定义优先级1
- struct cmp1{
- bool operator ()(int &a,int &b){
- return a>b;//最小值优先
- }
- };
- struct cmp2{
- bool operator ()(int &a,int &b){
- return a<b;//最大值优先
- }
- };
- //定义结构,使用运算符重载,自定义优先级2
- struct number1{
- int x;
- bool operator < (const number1 &a) const {
- return x>a.x;//最小值优先
- }
- };
- struct number2{
- int x;
- bool operator < (const number2 &a) const {
- return x<a.x;//最大值优先
- }
- };
- int a[]={,,,,,,,,,,,};
- number1 num1[]={,,,,,,,,,,,};
- number2 num2[]={,,,,,,,,,,,};
- int main()
- { priority_queue<int>que;//采用默认优先级构造队列
- priority_queue<int,vector<int>,cmp1>que1;//最小值优先
- priority_queue<int,vector<int>,cmp2>que2;//最大值优先
- priority_queue<int,vector<int>,greater<int> >que3;//注意“>>”会被认为错误,
- //这是右移运算符,所以这里用空格号隔开
- priority_queue<int,vector<int>,less<int> >que4;////最大值优先
- priority_queue<number1>que5;
- priority_queue<number2>que6;
- int i;
- for(i=;a[i];i++){
- que.push(a[i]);
- que1.push(a[i]);
- que2.push(a[i]);
- que3.push(a[i]);
- que4.push(a[i]);
- }
- for(i=;num1[i].x;i++)
- que5.push(num1[i]);
- for(i=;num2[i].x;i++)
- que6.push(num2[i]);
- printf("采用默认优先关系:\n(priority_queue<int>que;)\n");
- printf("Queue 0:\n");
- while(!que.empty()){
- printf("%3d",que.top());
- que.pop();
- }
- puts("");
- puts("");
- printf("采用结构体自定义优先级方式一:\n(priority_queue<int,vector<int>,cmp>que;)\n");
- printf("Queue 1:\n");
- while(!que1.empty()){
- printf("%3d",que1.top());
- que1.pop();
- }
- puts("");
- printf("Queue 2:\n");
- while(!que2.empty()){
- printf("%3d",que2.top());
- que2.pop();
- }
- puts("");
- puts("");
- printf("采用头文件\"functional\"内定义优先级:\n(priority_queue<int,vector<int>,greater<int>/less<int> >que;)\n");
- printf("Queue 3:\n");
- while(!que3.empty()){
- printf("%3d",que3.top());
- que3.pop();
- }
- puts("");
- printf("Queue 4:\n");
- while(!que4.empty()){
- printf("%3d",que4.top());
- que4.pop();
- }
- puts("");
- puts("");
- printf("采用结构体自定义优先级方式二:\n(priority_queue<number>que)\n");
- printf("Queue 5:\n");
- while(!que5.empty()){
- printf("%3d",que5.top());
- que5.pop();
- }
- puts("");
- printf("Queue 6:\n");
- while(!que6.empty()){
- printf("%3d",que6.top());
- que6.pop();
- }
- puts("");
- return ;
- }
- /*
- 运行结果 :
- 采用默认优先关系:
- (priority_queue<int>que;)
- Queue 0:
- 83 72 56 47 36 22 14 10 7 3
- 采用结构体自定义优先级方式一:
- (priority_queue<int,vector<int>,cmp>que;)
- Queue 1:
- 7 10 14 22 36 47 56 72 83 91
- Queue 2:
- 83 72 56 47 36 22 14 10 7 3
- 采用头文件"functional"内定义优先级:
- (priority_queue<int,vector<int>,greater<int>/less<int> >que;)
- Queue 3:
- 7 10 14 22 36 47 56 72 83 91
- Queue 4:
- 83 72 56 47 36 22 14 10 7 3
- 采用结构体自定义优先级方式二:
- (priority_queue<number>que)
- Queue 5:
- 7 10 14 22 36 47 56 72 83 91
- Queue 6:
- 83 72 56 47 36 22 14 10 7 3
- */
优先队列priority_queue的简单应用的更多相关文章
- 浅谈C++ STL中的优先队列(priority_queue)
从我以前的博文能看出来,我是一个队列爱好者,很多并不是一定需要用队列实现的算法我也会采用队列实现,主要是由于队列和人的直觉思维的一致性导致的. 今天讲一讲优先队列(priority_queue),实际 ...
- 9.优先队列,priority_queue
#include <iostream> #include <queue> #include <deque> #include <list> using ...
- 892A. Greed#贪婪(优先队列priority_queue)
题目出处:http://codeforces.com/problemset/problem/892/A 题目大意:有一些可乐(不一定装满),问能不能把所有可乐装进两个可乐瓶中 #include< ...
- C++ 优先队列priority_queue用法【转载】
priority_queue 对于基本类型的使用方法相对简单.他的模板声明带有三个参数,priority_queue<Type, Container, Functional>Type 为数 ...
- 【STL】优先队列priority_queue详解+OpenJudge-4980拯救行动
一.关于优先队列 队列(queue)这种东西广大OIer应该都不陌生,或者说,队列都不会你还学个卵啊(╯‵□′)╯︵┻━┻咳咳,通俗讲,队列是一种只允许从前端(队头)删除元素.从后端(队尾)插入元素的 ...
- 优先队列priority_queue的比较函数
STL头文件:#include<queue> 优先队列: 默认从大到小排列:priority_queuee<node>q; 自定义优先级的三种方法: 1.重载操作符: bool ...
- [转]c++优先队列(priority_queue)用法详解
既然是队列那么先要包含头文件#include <queue>, 他和queue不同的就在于我们可以自定义其中数据的优先级, 让优先级高的排在队列前面,优先出队 优先队列具有队列的所有特性, ...
- STL队列 之FIFO队列(queue)、优先队列(priority_queue)、双端队列(deque)
1.FIFO队列 std::queue就是普通意思上的FIFO队列在STL中的模版. 1.1主要的方法有: (1)T front():访问队列的对头元素,并不删除对头元素 (2)T back(): ...
- C++ STL 优先队列 priority_queue 详解(转)
转自https://blog.csdn.net/c20182030/article/details/70757660,感谢大佬. 优先队列 引入 优先队列是一种特殊的队列,在学习堆排序的时候就有所了解 ...
随机推荐
- Linux服务器在外地,如何用eclipse连接hdfs
配置外网和内网的映射,内部所有配置全部用内网的IP 本地所有配置皆为外网地址 本地给服务器发指令全部由映射转换为内网指定IP,即可
- JDBC连接MYSQL,批量执行SQL语句或在执行一个SQL语句之前执行一个SQL语句
conn = MysqlJdbcUtils.getConnection(); Statement ps=conn.createStatement(); ps.addBatch("trunca ...
- hibernate学习笔记(2)持久化类测试
持久化类的创建: 创建一个共有的不带参数的构造方法: public void Students(){ } 创建一个带参数的构造方法: (快捷键创建) 生成get,set方法: *可以不用此方法创建持久 ...
- TCP/IP 笔记 6 netstat -s 命令查看每个协议统计数据
netstat -s 命令,查看每个协议统计数据的常用方法 lenovo-myc@lenovomyc-Lenovo-Product:~$ netstat -s Ip: total packets re ...
- set源码之心得
C++的STL很强大,强大到我只愿慵懒地去使用而不知其所以然.直到李师问我,我的回答被李师否定,我方才意识到自己是多么地浅陋.希望自己有空抽时间把STL源码给研究一下,化为自己真正可以掌控的力量. s ...
- 【总结整理】webGIS学习
安装ArcGIS Server + ArcSDE + PostgreSQL + ArcMap安装(windows7)博客:https://blog.csdn.net/buqutianya/articl ...
- require()和include()代码重用
第五章 require()函数和include()函数几乎是相同的,二者唯一的区别在于函数失败后,require()函数将给出一个致命的错误,而include()只是给出一个警告. require_o ...
- 290. Word Pattern 单词匹配模式
[抄题]: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...
- ROS Learning-018 Arduino-For-ROS-003 (总结篇) 模板程序 即 如何运行
Arduino For ROS-003 - (总结篇) 模板程序 即 如何运行 我的Ubuntu系统:Ubuntu 14.04.10 TLS 32位 Arduino的版本:Arduino 1.6.11 ...
- loj2395 [JOISC 2017 Day 2]火车旅行
传送门 分析 我们知道无论往左走还是往右走一定都是往不低于这个点的地方走 于是我们可以考虑用倍增来维护一个点向左和向右走$2^i$最远分别能走到哪里 我们可以先用单调栈求出直走一步的情况,之后再处理倍 ...