在STL里有這個priority_queue,實現優先隊列的結構。在優先隊列中,優先級高的元素先出隊列。
現在在這里說說用法吧
先看看語法:
Syntax:
In their implementation in the C++ Standard Template Library, priority queues take three template parameters:1
2 template < class T, class Container = vector<T>,
class Compare = less<typename Container::value_type> > class priority_queue;
Where the template parameters have the following meanings:
T: Type of the elements.
Container: Type of the underlying container object used to store and access the elements.
Compare: Comparison class: A class such that the expression comp(a,b), where comp is an object of this class and a and b are elements of the container, returns true if a is to be placed earlier than b in a strict weak ordering operation. This can either be a class implementing a function call operator or a pointer to a function. This defaults to less<T>, which returns the same as applying the less-than operator (a<b).
The priority_queue object uses this expression when an element is inserted or removed from it (using push or pop, respectively) to grant that the element popped is always the greater in the priority queue.
可以自定義一個比較類,Compare
其實就三種用法
第一種,直接使用默認的。
它的模板聲明帶有三個參數,priority_queue<Type, Container, Functional>
Type 為數據類型, Container 為保存數據的容器,Functional 為元素比較方式。
Container 必須是用數組實現的容器,比如 vector, deque 但不能用 list.
STL里面默認用的是 vector. 比較方式默認用 operator< , 所以如果你把后面倆個
參數缺省的話,優先隊列就是大頂堆,隊頭元素最大。
看例子
priority_queue<int> qi;
int a[len] = {3,5,9,6,2};
priority_queue<int> qi;
for(i = 0; i < len; i++)
qi.push(a[i]);
for(i = 0; i < len; i++)
{
cout<<qi.top()<<" ";
qi.pop();
}
通過<操作符可知在整數中元素大的優先級高。
故例子中輸出結果為:9 6 5 3 2
第二種:
第二種方法:
在示例1中,如果我們要把元素從小到大輸出怎么辦呢?
這時我們可以傳入一個比較函數,使用functional.h函數對象作為比較函數。
如果要用到小頂堆,則一般要把模板的三個參數都帶進去。
STL里面定義了一個仿函數 greater<>,對於基本類型可以用這個仿函數聲明小頂堆
priority_queue<int, vector<int>, greater<int> >qi2;
對於自定義類型,則必須自己重載 operator< 或者自己寫仿函數
#include <iostream>
#include <queue>
using namespace std;
struct Node{
int x, y;
Node( int a= 0, int b= 0 ):
x(a), y(b) {}
};
bool operator<( Node a, Node b ){
if( a.x== b.x ) return a.y> b.y;
return a.x> b.x;
}
int main(){
priority_queue<Node> q;
for( int i= 0; i< 10; ++i )
q.push( Node( rand(), rand() ) );
while( !q.empty() ){
cout << q.top().x << ' ' << q.top().y << endl;
q.pop();
}
getchar();
return 0;
}
或者這樣定義也是能達到效果的:
struct Node{
int x, y;
Node( int a= 0, int b= 0 ):
x(a), y(b) {}
friend operator<( Node a, Node b ){
if( a.x== b.x ) return a.y> b.y;
return a.x> b.x;
}
};
自定義類型重載 operator< 后,聲明對象時就可以只帶一個模板參數。
但此時不能像基本類型這樣聲明
priority_queue<Node, vector<Node>, greater<Node> >;
原因是 greater<Node> 沒有定義,如果想用這種方法定義
則可以按如下方式
例子:
#include <iostream>
#include <queue>
using namespace std;
struct Node{
int x, y;
Node( int a= 0, int b= 0 ):
x(a), y(b) {}
};
struct cmp{
bool operator() ( Node a, Node b ){
if( a.x== b.x ) return a.y> b.y;
return a.x> b.x; }
};
int main(){
priority_queue<Node, vector<Node>, cmp> q;
for( int i= 0; i< 10; ++i )
q.push( Node( rand(), rand() ) );
while( !q.empty() ){
cout << q.top().x << ' ' << q.top().y << endl;
q.pop();
}
getchar();
return 0;
}
還有一點要注意的是priority_queue中的三個參數,后兩個可以省去,因為有默認參數,不過如果,有第三個參數的話,必定要寫第二個參數。