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 优先队列的更多相关文章

  1. STL priority_queue 优先队列 小记

    今天做题发现一个很有趣的地方,竟然还是头一次发现,唉,还是太菜了. 做图论用STL里的priority_queue去优化prim,由于特殊需求,我需要记录生成树中是用的哪些边. 于是,我定义的优先队列 ...

  2. STL - priority_queue(优先队列)

    参考:http://www.cnblogs.com/xzxl/p/7266404.html 一.基本定义: 优先队列容器与队列一样,只能从队尾插入元素,从队首删除元素.但是它有一个特性,就是队列中最大 ...

  3. c++ STL - priority_queue优先队列详解

    简述 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有最高级先出 (first in, l ...

  4. 【STL】优先队列priority_queue详解+OpenJudge-4980拯救行动

    一.关于优先队列 队列(queue)这种东西广大OIer应该都不陌生,或者说,队列都不会你还学个卵啊(╯‵□′)╯︵┻━┻咳咳,通俗讲,队列是一种只允许从前端(队头)删除元素.从后端(队尾)插入元素的 ...

  5. STL - priority_queue(优先队列)

    优先级队列priority_queue 最大值优先级队列.最小值优先级队列 优先级队列适配器 STL priority_queue 用来开发一些特殊的应用. priority_queue<int ...

  6. STL之优先队列

    STL 中优先队列的使用方法(priority_queu) 基本操作: empty() 如果队列为空返回真 pop() 删除对顶元素 push() 加入一个元素 size() 返回优先队列中拥有的元素 ...

  7. 详解C++ STL priority_queue 容器

    详解C++ STL priority_queue 容器 本篇随笔简单介绍一下\(C++STL\)中\(priority_queue\)容器的使用方法和常见的使用技巧. priority_queue容器 ...

  8. 第20章 priority_queue优先队列容器

    /* 第20章 priority_queue优先队列容器 20.1 priority_queue技术原理 20.2 priority_queue应用基础 20.3 本章小结 */ // 第20章 pr ...

  9. stack堆栈容器、queue队列容器和priority_queue优先队列容器(常用的方法对比与总结)

    stack堆栈是一个后进先出的线性表,插入和删除元素都在表的一端进行. stack堆栈的使用方法: 采用push()方法将元素入栈: 采用pop()方法将元素出栈: 采用top()方法访问栈顶元素: ...

随机推荐

  1. bps和pps

    bps,比特率指的是每秒传输比特数 在实际所说的1M带宽的意思是1Mbps(是兆比特每秒Mbps不是兆字节每秒MBps) pps(数据包每秒),常用的网络吞吐率的单位(即每秒发送多少个分组数据包),网 ...

  2. deepin安装nginx失败记录

    问题描述 在deepin系统中,apt install nginx 返回信息报错: nginx 依赖于 nginx-full (<< 1.10.3-1+deb9u2.1~) | nginx ...

  3. 2019-2020-1 20199305《Linux内核原理与分析》第五周作业

    系统调用的三层机制(上) (一)用户态.内核态和中断 (1)Intel x86 CPU有4种不同的执行级别 分别是0.1.2.3,数字越小,特权越高.Linux操作系统中只是采用了其中的0和3两个特权 ...

  4. WPF 获取系统 DPI 的多种方法

    原文:WPF 获取系统 DPI 的多种方法 WPF 获取系统 DPI 的多种方法 由于 WPF 的尺寸单位和系统的 DPI 相关,我们有时需要获取 DPI 值来进行一些界面布局的调整,本文汇总了一些 ...

  5. CF750G New Year and Binary Tree Paths(DP)

    神仙题.为啥我第一眼看上去以为是个普及题 路径有两种,第一种是从 LCA 一边下去的,第二种是从 LCA 两边都下去了的. 先考虑第一种. 先枚举路径长度 \(h\). 当 LCA 编号是 \(x\) ...

  6. postgresql 笔记

    客户端GUI 在官网下载一个,在安装的时候,不安装 server 端,会在客户端 安装一个 pgadmin .

  7. java之逻辑运算符

    &-逻辑与    |-逻辑或    !-逻辑非    &&-短路与    ||-短路或    ^-逻辑异或 a b a&b a|b !a a^b a&& ...

  8. 黑科技,利用python拨打电话,控制手机技术!

    跟selenium操作浏览器原理类似,这是用appium操作移动设备的一个自动化功能,自娱自乐,主要是通过小案例引出相关技术 很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手. 很 ...

  9. IT兄弟连 Java语法教程 数组 多维数组 二维数组的初始化

    二维数组的初始化与一位数组初始化类似,同样可以使用静态初始化或动态初始化. 1)静态初始化 静态初始化的格式如下: 数组名字 = new 数组元素的类型[][]{new 数组元素的类型[]{元素1,元 ...

  10. jsp模板

    <%String path = request.getContextPath();String basePath = request.getScheme()+"://"+re ...