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. 机器学习(1)——K近邻算法

    KNN的函数写法 import numpy as np from math import sqrt from collections import Counter def KNN_classify(k ...

  2. Python爬虫(requests模块)

     Requests是唯一的一个非转基因的Python HTTP库,人类可以安全享用. Requests基础学习 使用方法: 1.导入Requests模块: import requests 2.尝试用g ...

  3. 使用java代码操作Redis

    1导入pom.xml依赖 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis ...

  4. vue/cli2.0优化

    vue/cli2.0 脚手架 在项目写完了之后, 运行npm run build --report可以看出这个项目的资源占比情况.可以看出整个项目哪一个资源在整个项目占比最大.它会自动打开一个可视化的 ...

  5. C++ class内类型重载,operator Type()

    #include <iostream> // operator Type() 类型操作符重载 // operator int() // operator double() // ... / ...

  6. vue中is的使用

    :is作用 1.动态切换不同组件 <div id="app"> <button @click="changeComponent('component1' ...

  7. Codeforces Round #603 (Div. 2) F. Economic Difficulties dp

    F. Economic Difficulties An electrical grid in Berland palaces consists of 2 grids: main and reserve ...

  8. jmeter进行接口测试--csv参数化,数据驱动-转

    首先我们要有一个接口测试用例存放的地方,我们这里用EXCEL模板管理,里面包含用例编号.入参.优先级.请求方式.url等等. 1:新建一个txt文件,命名为sjqd,后缀名改为csv,右键excel格 ...

  9. laravel中控制器的创建和使用(五)

    laravel中我们可以使用 artisan 命令来帮助我们创建控制器文件. php artisan make:controller TestController TestController 控制器 ...

  10. rxjava介绍

    Observable 在RxJava1.x中,最熟悉的莫过于Observable这个类了,笔者刚使用RxJava2.x时,创建一个Observable后,顿时是懵逼的.因为我们熟悉的Subscribe ...