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()方法访问栈顶元素: ...
随机推荐
- SpringCloud单元测试【六】
SpringCloud的单元测试主要是依靠 Mock以及Mockito, 所以我们需要对Mock以及Mockito有一定的认识. 一.为什么要用MockMvc 可能我们在测试控制层的代码都是启动服务器 ...
- python程序封装成exe流程
在学习python的过程中,在IDE编写完成py项目,运行成功想要封装成exe,方便分享给不同的人即使别人没有安装python也可以使用. 封装的过程中遇到一些问题,记录一下,方便自己和他人查阅. 以 ...
- OpenFOAM——过渡管中的湍流
本算例来自<ANSYS Fluid Dynamics Verification Manual>中的VMFL016:Turbulent Flow in a Transition Duct 一 ...
- Feign、httpclient、OkHttp3 结合使用
疯狂创客圈 Java 高并发[ 亿级流量聊天室实战]实战系列 [博客园总入口 ] 疯狂创客圈 正在进行分布式和高并发基础原理 的研习,比如下面的一些基础性的内容: 一.Netty Redis 亿级流量 ...
- css样式的介绍
1.什么是css? 简单的来说css就是配合HTML的,HTML主要负责页面的结构,css就像一个美容师,主要负责页面的美化. 2.css的样式 css的样式有三种:行内样式 内部式 外部链接式 ...
- IT兄弟连 HTML5教程 HTML文档主体标记body
在HTML的<body>和</body>标记中定义文档的主体,包含文档的所有内容(比如文本.超链接.图像.表格和列表等等).<body>标签有自己的属性,设置< ...
- 如何使用numpy实现一个全连接神经网络?(上)
全连接神经网络的概念我就不介绍了,对这个不是很了解的朋友,可以移步其他博主的关于神经网络的文章,这里只介绍我使用基本工具实现全连接神经网络的方法. 所用工具: numpy == 1.16.4 matp ...
- poj-3682 King Arthur's Birthday Celebration
C - King Arthur's Birthday Celebration POJ - 3682 King Arthur is an narcissist who intends to spare ...
- [07]ASP.NET Core 进程外(out-of-process)托管
ASP.NET Core 进程外(out-of-process)托管 本文作者:梁桐铭- 微软最有价值专家(Microsoft MVP) 文章会随着版本进行更新,关注我获取最新版本 本文出自<从 ...
- Fusionstorage的逻辑架构
Fusionstorage Fusionstorage的逻辑架构 Mdc:元数据控制,实现对分布式集群的状态控制,以及控制数据分布式规则,数据重建规则等,mdc默认部署在3个节点的zk盘上,形成mdc ...