优先队列也是用 #include<queue>
这个头文件,且不必引入vector的头文件
基础类型优先级设置
默认定义:
+ priority_queue<int>
大顶堆
手工设置:
priority_queue<int, vector<int>, less<int>>
大顶堆:表示其他都比堆顶小priority_queue<int, vector<int>, greater<int>>
小顶堆:表示其他都比堆顶大
后面补充的两个参数:内部用容器+规则。
既然默认是大顶堆,所以手动设置时只用管小顶堆即可,greater!.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
结构体设置优先级:
只可在结构体内部重载小于号。
两种重置用法:
- 运算符重载 + 友元
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 比较运算符外置
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
调用语法是:priority_queue<fruit,vector<fruit> , cmp > q;
//这个和基本类型的用法就相似了,只不过是用cmp代替了less或者greater