优先级队列-堆-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 }

猜你喜欢

转载自www.cnblogs.com/jishuren/p/12264099.html