优先级队列-堆-STL实现

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <queue>
 4 
 5 using namespace std;
 6 
 7 // 默认是最大堆
 8 //
 9 
10 int main()
11 {
12     priority_queue<int> heap;
13     heap.push(3);
14     heap.push(1);
15     heap.push(5);
16     heap.push(4);
17 
18     while(!heap.empty())
19     {
20         cout<<heap.top()<<endl;
21         heap.pop();
22     }
23 
24 
25     // 如何实现最小堆?
26     priority_queue<int,vector<int>,greater<int>> heap2;
27     heap2.push(2);
28     heap2.push(4);
29     heap2.push(6);
30     heap2.push(1);
31     heap2.push(5);
32 
33     while(!heap2.empty())
34     {
35         cout<<heap2.top()<<endl;
36         heap2.pop();
37     }
38     return 0;
39 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值