priority_queue的用法
priority_queue本质是一个堆。
1. 头文件是#include<queue>
2. 关于priority_queue中元素的比较
模板申明带3个参数:priority_queue<Type, Container, Functional>,其中Type 为数据类型,Container为保存数据的容器,Functional 为元素比较方式。
Container必须是用数组实现的容器,比如vector,deque等等,但不能用 list。STL里面默认用的是vector。
2.1 比较方式默认用operator<,所以如果把后面2个参数缺省的话,优先队列就是大顶堆(降序),队头元素最大。特别注意pair的比较函数。
以下代码返回一个降序输出:
#include <iostream>
#include <queue>
using namespace std;
int main(){
priority_queue<int> q;
for( int i= ; i< ; ++i ) q.push(i);
while( !q.empty() ){
cout<<q.top()<<endl;
q.pop();
}
return ;
}
以下代代码返回pair的比较结果,先按照pair的first元素降序,first元素相等时,再按照second元素降序:
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int main(){
priority_queue<pair<int,int> > coll;
pair<int,int> a(,);
pair<int,int> b(,);
pair<int,int> c(,);
coll.push(c);
coll.push(b);
coll.push(a);
while(!coll.empty())
{
cout<<coll.top().first<<"\t"<<coll.top().second<<endl;
coll.pop();
}
return ;
}
2.2 如果要用到小顶堆,则一般要把模板的3个参数都带进去。STL里面定义了一个仿函数greater<>,基本类型可以用这个仿函数声明小顶堆。
以下代码返回一个升序输出:
#include <iostream>
#include <queue>
using namespace std;
int main(){
priority_queue<int, vector<int>, greater<int> > q;
for( int i= ; i< ; ++i ) q.push(-i);
while( !q.empty() ){
cout << q.top() << endl;
q.pop();
}
return ;
}
以下代代码返回pair的比较结果,先按照pair的first元素升序,first元素相等时,再按照second元素升序:
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int main(){
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > coll;
pair<int,int> a(,);
pair<int,int> b(,);
pair<int,int> c(,);
coll.push(c);
coll.push(b);
coll.push(a);
while(!coll.empty())
{
cout<<coll.top().first<<"\t"<<coll.top().second<<endl;
coll.pop();
}
return ;
}
2.3 对于自定义类型,则必须重载operator<或者重写仿函数。
2.3.1 重载operator<的例子:返回true时,说明左边形参的优先级低于右边形参
#include <iostream>
#include <queue>
using namespace std;
struct Node{
int x, y;
Node(int a=, int b=):
x(a),y(b){}
};
bool operator<(Node a, Node b){//返回true时,说明a的优先级低于b
//x值较大的Node优先级低(x小的Node排在队前)
//x相等时,y大的优先级低(y小的Node排在队前)
if( a.x== b.x ) return a.y> b.y;
return a.x> b.x;
}
int main(){
priority_queue<Node> q;
for( int i= ; i< ; ++i )
q.push( Node( rand(), rand() ) );
while( !q.empty() ){
cout << q.top().x << ' ' << q.top().y << endl;
q.pop();
}
return ;
}
自定义类型重载operator<后,声明对象时就可以只带一个模板参数。
但此时不能像基本类型这样声明priority_queue<Node,vector<Node>,greater<Node> >,原因是greater<Node>没有定义,如果想用这种方法定义则可以重载operator >。
例子:返回的是小顶堆。但不怎么用,习惯是重载operator<。
#include <iostream>
#include <queue>
using namespace std;
struct Node{
int x, y;
Node( int a= , int b= ):
x(a), y(b) {}
};
bool operator>( Node a, Node b ){//返回true,a的优先级大于b
//x大的排在队前部;x相同时,y大的排在队前部
if( a.x== b.x ) return a.y> b.y;
return a.x> b.x;
}
int main(){
priority_queue<Node,vector<Node>,greater<Node> > q;
for( int i= ; i< ; ++i )
q.push( Node( rand(), rand() ) );
while( !q.empty() ){
cout << q.top().x << ' ' << q.top().y << endl;
q.pop();
}
return ;
}
2.3.2 重写仿函数的例子(返回值排序与2.3.1相同,都是小顶堆。先按x升序,x相等时,再按y升序):
#include <iostream>
#include <queue>
using namespace std;
struct Node{
int x, y;
Node( int a= , int b= ):
x(a), y(b) {}
};
struct cmp{
bool operator() ( Node a, Node b ){//默认是less函数
//返回true时,a的优先级低于b的优先级(a排在b的后面)
if( a.x== b.x ) return a.y> b.y;
return a.x> b.x; }
};
int main(){
priority_queue<Node, vector<Node>, cmp> q;
for( int i= ; i< ; ++i )
q.push( Node( rand(), rand() ) );
while( !q.empty() ){
cout << q.top().x << ' ' << q.top().y << endl;
q.pop();
}
return ;
}
参考:http://www.cnblogs.com/flyoung2008/articles/2136485.html
priority_queue的用法的更多相关文章
- STL priority_queue 常见用法详解
<算法笔记>学习笔记 priority_queue 常见用法详解 //priority_queue又称优先队列,其底层时用堆来实现的. //在优先队列中,队首元素一定是当前队列中优先级最高 ...
- priority_queue 优先队列用法
//采用默认优先关系: //(priority_queue<int>que;) //Queue 0: // 91 83 72 56 47 36 22 14 10 7 3 // //采用结构 ...
- (转)priority_queue的用法
priority_queue调用 STL里面的 make_heap(), pop_heap(), push_heap() 算法实现,也算是堆的另外一种形式.先写一个用 STL 里面堆算法实现的与真正的 ...
- 【STL】-priority_queue的用法
初始化: priority_queue<int> maxPQ; priority_queue<int,vector<int& ...
- 【转】priority_queue的用法
http://www.cnblogs.com/flyoung2008/articles/2136485.html priority_queue调用 STL里面的 make_heap(), pop_he ...
- C++中priority_queue的用法
本来想自己写一写的,但看到这个随笔,感觉要写的东西跟这个差不多,就直接附上链接. 需要注意事项: rand()函数需要引入头文件#include<cstdlib>. 自定义类型,重载ope ...
- poj 3253 Fence Repair(模拟huffman树 + 优先队列)
题意:如果要切断一个长度为a的木条需要花费代价a, 问要切出要求的n个木条所需的最小代价. 思路:模拟huffman树,每次选取最小的两个数加入结果,再将这两个数的和加入队列. 注意priority_ ...
- 堆应用---构造Huffman树(C++实现)
堆: 堆是STL中priority_queue的最高效的实现方式(关于priority_queue的用法:http://www.cnblogs.com/flyoung2008/articles/213 ...
- NOIP经典基础模板总结
date: 20180820 spj: 距离NOIP还有81天 目录 STL模板: priority_queue 的用法:重载<,struct cmpqueue 的用法 stack 的用法vec ...
随机推荐
- 区域检测算法-MSERs
区域检测算法-MSERs:最大稳定极值区域 参考书籍——<图像局部不变性特征与描述>王永明.王贵锦著 MSER最大极值稳定区域的提取步骤:1.像素点排序 2.极值区域生成 3.稳定 ...
- 爬取QQ音乐(讲解爬虫思路)
一.问题描述: 本次爬取的对象是QQmusic,为自己后面做django音乐网站的开发获取一些资源. 二.问题分析: 由于QQmusic和网易音乐的方式差不多,都是讲歌曲信息放入到播放界面播放,在其他 ...
- vue公共
1 需求:在做项目的过程中发现,有一些功能是公共的,于是就想把这些公共的功能抽出来,做成独立的模块,别的项目需要用到,直接引用这个模块 2 问题: 前端:1 是用vue做的,vue的跳转是通过rout ...
- usb 枚举流程简介
1. 枚举是什么? 枚举就是从设备读取一些信息,知道设备是什么样的设备,如何进行通信,这样主机就可以根据这些信息来加载合适的驱动程序.调试USB设备,很重要的一点就是USB的枚举过程,只 ...
- elasticsearch 修改磁盘比例限制
命令为:下面也可以单独进行限制,官网说可以设置具体数值,但是无没成功,有成功的大神可以在下面告诉我一下哈 PUT _cluster/settings{ "persistent": ...
- python random库
random模块 >>> import random #随机小数 >>> random.random() # 大于0且小于1之间的小数 0.766433866365 ...
- 20155205 郝博雅 Exp 8 Web基础
20155205 郝博雅 Exp 8 Web基础 一.实验目标 (1).Web前端HTML(0.5分) 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个含有表单 ...
- POJ2947-Widget Factory
工厂里每件期间的生产时间为3-9天,告诉你有N个器件和M个计划,每个计划都是说明生产1-N号器件的时间,最后问你每件器件的生产时间.或者多解或没有解. 例如样例 2 3 2 MON THU 1 2 3 ...
- Linux下Memcache服务器端的安装
最近在研究怎么让Discuz!去应用Memcache去做一些事情,记录下Memcache安装的过程. Linux下Memcache服务器端的安装服务器端主要是安装memcache服务器端,目前的最新版 ...
- Key Technologies Primer 读书笔记,翻译 --- Struct 学习 1
原文链接:https://struts.apache.org/primer.html 本来想写成读书笔记的,结果还是变成翻译,谨作记录,学习. 1.HTML -- 见我前面文章 2.Interne ...