priority_queue的用法

轉載於:https://www.cnblogs.com/Deribs4/p/5657746.html

 

priority_queue本質是一個堆。

1. 頭文件是#include<queue>

2. 關於priority_queue中元素的比較

  模板申明帶3個參數:priority_queue<Type, Container, Functional>,其中Type 爲數據類型,Container爲保存數據的容器,Functional 爲元素比較方式。

  Container必須是用數組實現的容器,比如vector,deque等等,但不能用 list。STL裏面默認用的是vector。

2.1 比較方式默認用operator<,所以如果把後面2個參數缺省的話,優先隊列就是大頂堆(降序),隊頭元素最大。特別注意pair的比較函數

  以下代碼返回一個降序輸出:

 1 #include <iostream>
 2 #include <queue>
 3 using namespace std;
 4 int main(){
 5     priority_queue<int> q;
 6     for( int i= 0; i< 10; ++i ) q.push(i);
 7     while( !q.empty() ){
 8         cout<<q.top()<<endl;
 9         q.pop();
10     }
11     return 0;
12 }

 

  以下代代碼返回pair的比較結果,先按照pair的first元素降序,first元素相等時,再按照second元素降序:

 1 #include<iostream>
 2 #include<vector>
 3 #include<queue>
 4 using namespace std;
 5 int main(){
 6     priority_queue<pair<int,int> > coll;
 7     pair<int,int> a(3,4);
 8     pair<int,int> b(3,5);
 9     pair<int,int> c(4,3);
10     coll.push(c);
11     coll.push(b);
12     coll.push(a);
13     while(!coll.empty())
14     {
15         cout<<coll.top().first<<"\t"<<coll.top().second<<endl;
16         coll.pop();
17     }
18     return 0;
19 }

 

 

2.2 如果要用到小頂堆,則一般要把模板的3個參數都帶進去。STL裏面定義了一個仿函數greater<>,基本類型可以用這個仿函數聲明小頂堆

  以下代碼返回一個升序輸出:

 1 #include <iostream>
 2 #include <queue> 
 3 using namespace std;
 4 int main(){
 5     priority_queue<int, vector<int>, greater<int> > q;
 6     for( int i= 0; i< 10; ++i ) q.push(10-i);
 7     while( !q.empty() ){
 8         cout << q.top() << endl;
 9         q.pop();
10     }
11     return 0;
12 }

 

  以下代代碼返回pair的比較結果,先按照pair的first元素升序,first元素相等時,再按照second元素升序: 

 1 #include<iostream>
 2 #include<vector>
 3 #include<queue>
 4 using namespace std;
 5 int main(){
 6     priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > coll;
 7     pair<int,int> a(3,4);
 8     pair<int,int> b(3,5);
 9     pair<int,int> c(4,3);
10     coll.push(c);
11     coll.push(b);
12     coll.push(a);
13     while(!coll.empty())
14     {
15         cout<<coll.top().first<<"\t"<<coll.top().second<<endl;
16         coll.pop();
17     }
18     return 0;
19 }

 

 

2.3 對於自定義類型,則必須重載operator<或者重寫仿函數。

2.3.1 重載operator<的例子:返回true時,說明左邊形參的優先級低於右邊形參

 1 #include <iostream>
 2 #include <queue> 
 3 using namespace std;
 4 struct Node{
 5     int x, y;
 6     Node(int a=0, int b=0):
 7         x(a),y(b){}
 8 };
 9 bool operator<(Node a, Node b){//返回true時,說明a的優先級低於b
10     //x值較大的Node優先級低(x小的Node排在隊前)
11     //x相等時,y大的優先級低(y小的Node排在隊前)
12     if( a.x== b.x ) return a.y> b.y;
13     return a.x> b.x; 
14 }
15 int main(){
16     priority_queue<Node> q;
17     for( int i= 0; i< 10; ++i )
18     q.push( Node( rand(), rand() ) );
19     while( !q.empty() ){
20         cout << q.top().x << ' ' << q.top().y << endl;
21         q.pop();
22     }
23     return 0;
24 }

 

自定義類型重載operator<後,聲明對象時就可以只帶一個模板參數

但此時不能像基本類型這樣聲明priority_queue<Node,vector<Node>,greater<Node> >,原因是greater<Node>沒有定義,如果想用這種方法定義則可以重載operator >。

例子:返回的是小頂堆。但不怎麼用,習慣是重載operator<。

 1 #include <iostream>
 2 #include <queue>
 3 using namespace std;
 4 struct Node{
 5     int x, y;
 6     Node( int a= 0, int b= 0 ):
 7         x(a), y(b) {}
 8 };
 9 bool operator>( Node a, Node b ){//返回true,a的優先級大於b
10     //x大的排在隊前部;x相同時,y大的排在隊前部
11     if( a.x== b.x ) return a.y> b.y;
12     return a.x> b.x; 
13 }
14 int main(){
15     priority_queue<Node,vector<Node>,greater<Node> > q;
16     for( int i= 0; i< 10; ++i )
17     q.push( Node( rand(), rand() ) );
18     while( !q.empty() ){
19         cout << q.top().x << ' ' << q.top().y << endl;
20         q.pop();
21     }
22     return 0;
23 }

 

2.3.2 重寫仿函數的例子(返回值排序與2.3.1相同,都是小頂堆。先按x升序,x相等時,再按y升序):

 1 #include <iostream>
 2 #include <queue>
 3 using namespace std;
 4 struct Node{
 5     int x, y;
 6     Node( int a= 0, int b= 0 ):
 7         x(a), y(b) {}
 8 };
 9 struct cmp{
10     bool operator() ( Node a, Node b ){//默認是less函數
11         //返回true時,a的優先級低於b的優先級(a排在b的後面)
12         if( a.x== b.x ) return a.y> b.y;      
13         return a.x> b.x; }
14 };
15 int main(){
16     priority_queue<Node, vector<Node>, cmp> q;
17     for( int i= 0; i< 10; ++i )
18     q.push( Node( rand(), rand() ) );
19     while( !q.empty() ){
20         cout << q.top().x << ' ' << q.top().y << endl;
21         q.pop();
22     }
23     return 0;
24 } 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章