博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
5.1 priority_queue使用
阅读量:7250 次
发布时间:2019-06-29

本文共 1189 字,大约阅读时间需要 3 分钟。

hot3.png

1:基本类型,priority_queue定义时只指定第一个参数,默认是小顶堆;指定第三个参数为less<>就是大顶堆了

2:自定义类型,priority_queue必须指定全部三个参数,比较函数也要定义成个结构体struct cmp,里面定义一个

bool operator( )( arg1, arg2){ ....};

#include 
#include 
using namespace std;int n;//1: 基本类型//priority_queue
 q;//仅有第一个参数,缺省后两个,默认是大顶堆//priority_queue
, less
> q;//使用less<>, 大顶堆, priority_queue
, greater
> q;//使用greater<>, 小顶堆//2: 自定义类型typedef struct node{ int data;};typedef struct cmp{ bool operator()(node a, node b){//注意operator后面跟着() return a.data > b.data; }};//priority_queue
 qn1;//不支持! //priority_queue
, cmp> qn;//cmp里使用
<比较, 大顶堆priority_queue>
, cmp> qn;//cmp用>, 小顶堆int main(){ //1: 基本类型 /*q.push(5); q.push(13); q.push(6); while(!q.empty()){ printf("%d\n",q.top()); q.pop(); }*/ //2: 自定义类型   node n1; n1.data = 17; qn.push(n1); node n2; n2.data = 4; qn.push(n2); node n3; n3.data = 9; qn.push(n3); while(!qn.empty()){ printf("%d\n",qn.top()); qn.pop(); } return 0;}

ref:

转载于:https://my.oschina.net/kaneiqi/blog/308491

你可能感兴趣的文章
配置云存储网关在线服务支持多个互联VPC-高速通道版
查看>>
6个步骤从头开始编写机器学习算法:感知器案例研究
查看>>
NCalc 学习笔记 (三)
查看>>
NetBeans 成为 Apache 软件基金会顶级项目
查看>>
SSRF在Redis中反弹shell
查看>>
UML关系图
查看>>
SpringBoot 手写切片/面向切面编程
查看>>
动态 Web Server 技术发展历程
查看>>
使用pymysql(使用一)
查看>>
Redisson 3.10.6 发布,Redis 客户端
查看>>
日志框架 - 基于spring-boot - 使用入门
查看>>
用libtommath实现RSA算法
查看>>
基于POLARDB数据库的压测实践
查看>>
通过工具SecureCRTPortable将项目部署到服务器上
查看>>
利用QRCode实现待logo的二维码的创建
查看>>
【云周刊】第190期:阿里云超算揭秘:虚拟机的心脏,物理机的肌肉
查看>>
崩溃bug日志总结3
查看>>
推荐一个有趣的Chrome扩展程序-查看任意网站的开发技术栈
查看>>
shell技巧5 - 全自动打包ipa
查看>>
uC/OS-II源码分析(六)
查看>>