首页 > 代码库 > 优先队列

优先队列

 1 #include <iostream>
 2 #include <queue>
 3 #include <vector>
 4 using namespace std;
 5 
 6 struct cmp
 7 {
 8     bool operator()( const int &a, const int &b )
 9     {
10         return a < b;
11     }
12 };
13 
14 int main()
15 {
16     //priority_queue< int, vector<int>, greater<int> > q;
17     //priority_queue<int> q;
18     //priority_queue< int, vector<int>, less<int> > q;
19     priority_queue< int, vector<int>, cmp > q;
20     int n;
21     cin >> n;
22     while(n--)
23     {
24         int cmp;
25         cin >> cmp;
26         q.push(cmp);
27     }
28     while(!q.empty())
29     {
30         cout << q.top() << endl;
31         q.pop();
32     }
33     return 0;
34 }
35 //4006

 

优先队列