C++ STL priority_queue 优先队列
1.基本概念
优先队列: 与队列的用法是一样的,优先队列内部是通过堆排序实现的。
因为push和pop方法会导致元素变化,故而需要重新调整堆,而top是把堆顶元素输出。
priority_queue< type, container, function >
- type:数据类型;
- container:实现优先队列的底层容器;
- function:元素之间的比较方式;默认写法是大顶堆,对应输出是逆序数组。
2.存储int型写法
#include <iostream>
#include <queue>
using namespace std;
int main(){
//默认是大顶堆
priority_queue<int> large; // 这两种写法是相同的 priority_queue<int,vector<int>, less<int>> large; // 第一个int表示队列的元素类型,这里一定要有空格,不然成了右移运算符
priority_queue<int, vector<int>, greater<int> > small; //小顶堆,升序 for (int i = ; i < ; i++){
large.push(i);
small.push(i);
}
while (!large.empty()){
cout << large.top() << ' ';
large.pop();
}
//输出结果:4 3 2 1 0
cout << endl; while (!small.empty()){
cout << small.top() << ' ';
small.pop();
}
//输出结果:0 1 2 3 4
return ;
}
3.存储pair<int,int>型写法
先按照pair的first元素排序,first元素相等时,再按照second元素排序
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int main(){
priority_queue<pair<int,int> > pq;//大顶堆
//priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>> > small;//小顶堆
pair<int,int> a(,);
pair<int,int> b(,);
pair<int,int> c(,);
pq.push(c);
pq.push(b);
pq.push(a);
while(!pq.empty()) {
cout<<pq.top().first<<"\t"<<pq.top().second<<endl;
pq.pop();
}
return ;
}
通过构建仿函数,实现自定义的排序方式;先按照pair的second元素排序,second元素相等时,再按照first元素排序
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
struct cmp{
bool operator()(pair<int,int> a,pair<int,int> b){
if(a.second==b.second) return a.first>a.first;
else return a.second>b.second;
}
};
int main(){
priority_queue<pair<int,int>,vector<pair<int,int>>,cmp> pq;//大顶堆
//priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>> > small;//小顶堆
pair<int,int> a(,);
pair<int,int> b(,);
pair<int,int> c(,);
pq.push(c);
pq.push(b);
pq.push(a);
while(!pq.empty()) {
cout<<pq.top().first<<"\t"<<pq.top().second<<endl;
pq.pop();
}
return ;
}
4.自定义类型的写法
有两种方式,一种是重载<或>,还有一种是重写仿函数,对应的代码如下
#include "queue"
#include"vector"
#include"iostream"
#include"algorithm"
using namespace std;
class Node {
public:
int x;
int y;
Node(int _x, int _y): x(_x), y(_y){}
};
bool operator<(Node a,Node b){//重写运算符<,对应的是less
return a.x<b.x;
}
struct cmp{//重写仿函数
bool operator()(Node a,Node b){
return a.y>b.y;
}
};
int main(){
priority_queue<Node,vector<Node>,less<Node>> pq;//大顶堆
//priority_queue<Node,vector<Node>,cmp> pq;//这种写法效果同上
Node a(,);
Node b(,);
Node c(,);
pq.push(c);
pq.push(b);
pq.push(a);
while(!pq.empty()) {
cout<<pq.top().x<<"\t"<<pq.top().y<<endl;
pq.pop();
}
return ;
}
leetcode 优先队列示例:
https://github.com/AntonioSu/leetcode/blob/master/problems/239.SlidingWindowMaximum.md
https://github.com/AntonioSu/leetcode/blob/master/problems/295.FindMedianfromDataStream.md
C++ STL priority_queue 优先队列的更多相关文章
- STL priority_queue 优先队列 小记
今天做题发现一个很有趣的地方,竟然还是头一次发现,唉,还是太菜了. 做图论用STL里的priority_queue去优化prim,由于特殊需求,我需要记录生成树中是用的哪些边. 于是,我定义的优先队列 ...
- STL - priority_queue(优先队列)
参考:http://www.cnblogs.com/xzxl/p/7266404.html 一.基本定义: 优先队列容器与队列一样,只能从队尾插入元素,从队首删除元素.但是它有一个特性,就是队列中最大 ...
- c++ STL - priority_queue优先队列详解
简述 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有最高级先出 (first in, l ...
- 【STL】优先队列priority_queue详解+OpenJudge-4980拯救行动
一.关于优先队列 队列(queue)这种东西广大OIer应该都不陌生,或者说,队列都不会你还学个卵啊(╯‵□′)╯︵┻━┻咳咳,通俗讲,队列是一种只允许从前端(队头)删除元素.从后端(队尾)插入元素的 ...
- STL - priority_queue(优先队列)
优先级队列priority_queue 最大值优先级队列.最小值优先级队列 优先级队列适配器 STL priority_queue 用来开发一些特殊的应用. priority_queue<int ...
- STL之优先队列
STL 中优先队列的使用方法(priority_queu) 基本操作: empty() 如果队列为空返回真 pop() 删除对顶元素 push() 加入一个元素 size() 返回优先队列中拥有的元素 ...
- 详解C++ STL priority_queue 容器
详解C++ STL priority_queue 容器 本篇随笔简单介绍一下\(C++STL\)中\(priority_queue\)容器的使用方法和常见的使用技巧. priority_queue容器 ...
- 第20章 priority_queue优先队列容器
/* 第20章 priority_queue优先队列容器 20.1 priority_queue技术原理 20.2 priority_queue应用基础 20.3 本章小结 */ // 第20章 pr ...
- stack堆栈容器、queue队列容器和priority_queue优先队列容器(常用的方法对比与总结)
stack堆栈是一个后进先出的线性表,插入和删除元素都在表的一端进行. stack堆栈的使用方法: 采用push()方法将元素入栈: 采用pop()方法将元素出栈: 采用top()方法访问栈顶元素: ...
随机推荐
- go语言设计模式之memento
memento.go package memento import ( "fmt" ) type State struct { Description string } type ...
- uva 10189 扫雷
简单的输入 判断周围上下左右组合的八个方向的雷 然后输出 代码 #include <iostream> #include <memory.h> using namespace ...
- 2019.08.06模拟赛T2
题目大意: 已知三个$n$位二进制数$A$,$B$,$C$. 满足: $A+B=C$ 它们二进制位中$1$的个数分别为$a$,$b$,$c$. 求满足条件的最小的$C$. Solution 唉,又是一 ...
- python--基础知识点梳理(之数据结构)
数据结构: # 按逻辑结构(面向问题)分为:集合结构.线性结构.树形结构.图形结构 # 按物理结构(面向计算机)分为: # 顺序存储结构(把数据元素放在地址连续的存储单元中,数据间的逻辑关系和物理关系 ...
- PHP 构造函数
在PHP5以前的版本中,构造函数的名称必须与类名相同,这种方法在PHP5中仍然可以使用,但现在已经很少有人用了.PHP5以及之后的版本,构造函数用__construct()方法来声明,这样做的好处是可 ...
- RMAN详细教程(三):备份脚本的组件和注释
RMAN详细教程(一):基本命令代码 RMAN详细教程(二):备份.检查.维护.恢复 RMAN详细教程(三):备份脚本的组件和注释 RMAN详细教程(四):备份脚本实战操作 一.基本组件: 1.Ser ...
- Django多进程滚动日志的问题
使用RotatingFileHandler控制日志文件的大小 # settings.py LOGGING = { ... 'handlers': { ... 'file': { 'level': 'I ...
- [IDA]修改变量类型、删除变量名
1. 双击变量 2. 按D转换类型(Word.Byte.Dword) 3. 按U删除变量名 4. 按N修改变量名
- 【语义分割】Stacked Hourglass Networks 以及 PyTorch 实现
Stacked Hourglass Networks(级联漏斗网络) 姿态估计(Pose Estimation)是 CV 领域一个非常重要的方向,而级联漏斗网络的提出就是为了提升姿态估计的效果,但是其 ...
- 如何真正实现由文档驱动的API设计?
前言 本文主要介绍了一种新的开发思路:通过反转开发顺序,直接从API文档中阅读代码.作者认为通过这种开发方式,你可以更清楚地知道文档表达出什么以及它应该如何实现. 如果单从API文档出发,由于信息量不 ...