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 ...
随机推荐
- 每日一练ACM
2019.04.15 第1000题:A+B Problem Problem DescriptionCalculate A + B. InputEach line will contain two in ...
- Unity编辑器扩展-Custom List, displaying data your way
本文转自http://catlikecoding.com/unity/tutorials/editor/custom-list/ Custom List, displaying data your w ...
- python jieba库
https://www.cnblogs.com/snailclimb/p/9086433.html https://blog.csdn.net/codejas/article/details/8035 ...
- stm32f10x_it.c、stm32f10x_it.h和stm32f10x_conf.h文件作用
如上图,在STM32的Keil工程文件(Project)中一般都包含stm32f10x_it.c.stm32f10x_it.h和stm32f10x_conf.h这三个文件,但是在ST官方提供的标准库“ ...
- openXML向Word插入表
表是 Word 中的另一类型的块级内容,它是以行和列排列的一组段落(以及其他块级内容). Word 中的表格通过 tbl 元素定义,该元素类似于 HTML <表格>标记. 表元素指定文档中 ...
- Centos 安装 mysql tar.gz
http://www.cnblogs.com/coderls/p/6848873.html
- [XAF] Llamachant Framework Modules
Llamachant Framework Modules 最近更新 2018-08-22 *变更:我们从所需的模块列表中删除了审计跟踪模块.如果要在应用程序中使用Audit Trail功能,请将Aud ...
- JS中获取CSS样式的方法
1.对于内联样式,可以直接使用ele.style.属性名(当然也可以用键值对的方式)获得.注意在CSS中单词之间用-连接,在JS中要用驼峰命名法 如 <div id="dv" ...
- 包建强的培训课程(17):Java代码敏捷之道
第1讲 千言万语聊注释 按图索骥 奇葩注释“赏析” Git提交的学问 第2讲 RxJava:函数式编程 从一只猫的故事说起 背压 第3讲 代码瘦身 抽象相同逻辑的代码 查找相似代码 AOP一瞥 第4讲 ...
- Win10下python不同版本同时安装并解决pip共存问题
特别说明,本文是在Windows64位系统下进行的,32位系统请下载相应版本的安装包,安装方法类似. 使用python开发,环境有Python2和 python3 两种,有时候需要两种环境切换使用,下 ...