62
技術社區[雲棲]
[算法係列之四]優先級隊列
【概念】
優先級隊列,顧名思義,就是一種根據一定優先級存儲和取出數據的隊列。它可以說是隊列和排序的完美結合體,不僅可以存儲數據,還可以將這些數據按照我們設定的規則進行排序。優先級隊列是堆的一種常見應用。有最大優先級隊列(最大堆)和最小優先級隊列(最小堆)。優先級隊列是一種維護有一組元素構成的集合S的數據結構。
【優先隊列支持的基本運算】
- //建立一個保存元素為int的優先級隊列,其實是建了一個小頂堆
- //但是請特別注意這樣的建的堆默認是大頂堆,即我們從堆頂去的元素是整個堆中元素最大的。
- priority_queue<int> Heap;
- //可以這樣建一個表示小頂堆的優先級隊列
- priority_queue<int , vector<int>, greater<int> > Heap;
- //將元素x放入優先級隊列中
- Heap.push(x);
- //取出優先級隊列第一個元素(堆頂元素),保存在x中
- int x = Heap.top();
- //彈出堆頂元素,取出後堆會自動調整為一個最小堆(最大堆)
- Heap.pop();
- //判斷是否為空
- Heap.empty();
- //頭文件
- #include<queue>
【原理】
priority_queue調用 STL裏麵的 make_heap(), pop_heap(), push_heap() 算法實現,也算是堆的另外一種形式。
用make_heap(), pop_heap(), push_heap() 簡單實現一個最大優先級隊列
/********************************* * 日期:2015-01-06 * 作者:SJF0115 * 題目: 簡單實現最大優先級隊列 * 博客: **********************************/ #include <iostream> #include <algorithm> #include <vector> using namespace std; //簡單實現最大優先級隊列 template<typename T> class priority_queue{ private: // 數據 vector<T> data; public: // 進隊列 void push(T val){ data.push_back(val); push_heap(data.begin(),data.end()); } // 出隊列 void pop(){ pop_heap(data.begin(),data.end()); data.pop_back(); } // 頭元素 T top(){ return data.front(); } // 大小 int size(){ return data.size(); } // 是否為空 bool empty(){ return data.empty(); } }; int main(){ priority_queue<char> heap; heap.push('5'); heap.push('4'); heap.push('3'); heap.push('9'); heap.push('6'); while(!heap.empty()){ cout<<heap.top()<<endl; heap.pop(); }//while }
STL裏麵的 priority_queue 寫法與此相似,隻是增加了模板及相關的迭代器什麼的。
priority_queue 對於基本類型的使用方法相對簡單。他的模板聲明帶有三個參數:
priority_queue<Type, Container, Functional>
其中Type 為數據類型, Container 為保存數據的容器,Functional 為元素比較方式。
Container 必須是用數組實現的容器,比如 vector, deque 但不能用 list.
STL裏麵默認用的是 vector. 比較方式默認用 operator< , 所以如果你把後麵倆個參數缺省的話,
優先隊列就是大頂堆,隊頭元素最大。
#include <iostream> #include <queue> using namespace std; int main(){ priority_queue<char> heap; heap.push('5'); heap.push('4'); heap.push('3'); heap.push('9'); heap.push('6'); // 輸出最大優先級隊列 while(!heap.empty()){ cout<<heap.top()<<endl; heap.pop(); }//while }
如果要用到小頂堆,則一般要把模板的三個參數都帶進去。
STL裏麵定義了一個仿函數 greater<>,對於基本類型可以用這個仿函數聲明小頂堆
#include <iostream> #include <queue> using namespace std; int main(){ // 最小優先級隊列 priority_queue<char,vector<char>,greater<char> > heap; heap.push('5'); heap.push('4'); heap.push('3'); heap.push('9'); heap.push('6'); // 輸出最大優先級隊列 while(!heap.empty()){ cout<<heap.top()<<endl; heap.pop(); }//while }
對於自定義類型,則必須自己重載 operator< 或者自己寫仿函數
【自定義優先級】
重載 operator<
- #include<iostream>
- #include<stdio.h>
- #include<queue>
- using namespace std;
- struct Node
- {
- //值
- int value;
- //編號
- int key;
- //重載操作符
- friend bool operator < (Node node1,Node node2)
- {
- //最大優先隊列
- return node1.value < node2.value;
- }
- /*
- 不要重載這個'>'隻重載'<'
- friend bool operator > (Node node1,Node node2)
- {
- return node1.value > node2.value;
- }
- */
- };
- struct Node2
- {
- //值
- int value;
- //編號
- int key;
- //重載操作符
- friend bool operator < (Node2 node1,Node2 node2)
- {
- //最小優先隊列
- return node1.value > node2.value;
- }
- };
- int main(){
- int i;
- //實例一 結構體1
- Node b[5];
- b[0].value = 6; b[0].key = 1;
- b[1].value = 9; b[1].key = 2;
- b[2].value = 2; b[2].key = 3;
- b[3].value = 8; b[3].key = 4;
- b[4].value = 1; b[4].key = 5;
- //最大優先隊列
- priority_queue<Node> Heap;
- //入隊列
- for(i = 0;i < 5;i++){
- Heap.push(b[i]);
- }
- printf("最大優先隊列:\n");
- //出隊列
- for(i = 0;i < 5;i++){
- printf("key:%d value:%d\n",Heap.top().key,Heap.top().value);
- Heap.pop();
- }
- //實例二 結構體2
- Node2 b2[5];
- b2[0].value = 6; b2[0].key = 1;
- b2[1].value = 9; b2[1].key = 2;
- b2[2].value = 2; b2[2].key = 3;
- b2[3].value = 8; b2[3].key = 4;
- b2[4].value = 1; b2[4].key = 5;
- //最大優先隊列
- priority_queue<Node2> Heap2;
- //入隊列
- for(i = 0;i < 5;i++){
- Heap2.push(b2[i]);
- }
- printf("最小優先隊列:\n");
- //出隊列
- for(i = 0;i < 5;i++){
- printf("key:%d value:%d\n",Heap2.top().key,Heap2.top().value);
- Heap2.pop();
- }
- return 0;
- }

注意:
- struct Node
- {
- //值
- int value;
- //編號
- int key;
- friend bool operator > (Node node1,Node node2)
- {
- return node1.value > node2.value;
- }
- };
這樣會報錯。因為標準庫默認使用元素類型的<操作符來確定它們之間的優先級關係。
自定義類型重載 operator< 後,聲明對象時就可以隻帶一個模板參數。
但此時不能像基本類型這樣聲明priority_queue<Node, vector<Node>, greater<Node> >;
原因是 greater<Node> 沒有定義,如果想用這種方法定義則可以按如下方式:
#include <iostream> #include <queue> using namespace std; struct Node{ int value; int key; Node(int x,int y):key(x),value(y){} }; struct cmp{ bool operator()(Node a,Node b){ if(a.key == b.key){ return a.value > b.value; } return a.key > b.key; } }; int main(){ priority_queue<Node,vector<Node>,cmp> heap; Node node0(5,6); Node node1(3,3); Node node2(2,4); Node node3(2,3); Node node4(1,3); heap.push(node0); heap.push(node1); heap.push(node2); heap.push(node3); heap.push(node4); while(!heap.empty()){ Node node = heap.top(); cout<<"Key->"<<node.key<<" Value->"<<node.value<<endl; heap.pop(); }//while }
具體實例:點擊打開鏈接
最後更新:2017-04-03 12:56:30