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()方法访问栈顶元素: ...
随机推荐
- Python踩坑系列之使用redis报错:module 'redis' has no attribute 'Redis'问题
初次使用redis时,在链接Redis后,运行报错“module 'redis' has no attribute 'Redis' ”. 具体代码如下: import redis r = redis. ...
- LRU hashMap(拉链) + 双向链表 java实现
//基于 hash (拉链法) + 双向链表,LRUcache //若改为开放寻址,线性探测法能更好使用cpuCache public class LRU { private class Node { ...
- vue预览本地图片
<template> <div> <a href="javascript:void(0);" @change="addImage" ...
- AcWing 801. 二进制中1的个数
网址 https://www.acwing.com/solution/AcWing/content/2066/ 题目描述给定一个长度为n的数列,请你求出数列中每个数的二进制表示中1的个数. 算法1主要 ...
- acwing 167. 木棒
乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位. 然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度. 请你设计一个程序,帮助乔 ...
- Mac下MongoDB配置与操作
1.环境配置 Xcode安装 2.下载安装包 官网地址是:MongoDB Download Center | MongoDB 3.解压文件, 将文件放置/usr/local 4.配置环境变量 open ...
- 第04组 Beta冲刺(4/5)
队名:new game 组长博客 作业博客 组员情况 鲍子涵(队长) 过去两天完成了哪些任务 地图移动 接下来的计划 素材和脚本相连 引入声音素材 还剩下哪些任务 让游戏本体运行 遇到了哪些困难 时间 ...
- vue的基础概念和语法01
vue的特点和web开发中的常见高级功能 解耦视图和数据 可复用的组件 前端路由技术 状态管理 虚拟DOM 数据响应式 不是所有元素操作都Vue都会监听并实现数据响应式 //push方法:追加 thi ...
- Java多线程并发面试问答
Java并发面试问答 什么是原子操作?Java并发API中的原子类是什么? 原子操作在单个任务单元中执行,而不受其他操作的干扰.在多线程环境中,原子操作是必需的,以避免数据不一致. int++不是原子 ...
- 【nginx启动报错】重启服务器之后nginx启动错
错误信息: # ./nginx nginx: [emerg] open() "/var/run/nginx/nginx.pid" failed (2: No such file ...