STL頭文件:#include<queue>
優先隊列:
默認從大到小排列:priority_queuee<node>q;
自帶的比較函數
priority_queue<int, vector<int>, less<int> > q;//等價於默認,從大到小排 //greater<int> 從小到大排
自定義優先級的三種方法:
1.重載操作符:
bool operator < (const node &a, const node &b) { return a.value < b.value; // 按照value從大到小排列 } priority_queue<node>q;
(const node &a是用引用傳遞,比按值傳遞node a效率更高,效果是一樣的)
2.自定義比較函數模板結構:
struct cmp{ bool operator ()(const node &a, const node &b) { return a.value>b.value;// 按照value從小到大排列 } }; priority_queue<node, vector<node>, cmp>q;
3.定義友元操作類重載函數
struct node{ int value; friend bool operator<(const node &a,const node &b){ return a.value<b.value; //按value從大到小排列 } }; priority_queue<node>q;
