介紹:
普通的隊列是一種先進先出的數據結構,元素在隊列尾追加,而從隊列頭刪除。
在優先隊列中,元素被賦予優先級。當訪問元素時,具有最高優先級的元素最先刪除。優先隊列具有最高級先出 (first in, largest out)的行為特征。
首先要包含頭文件#include<queue>
, 他和queue
不同的就在於我們可以自定義其中數據的優先級, 讓優先級高的排在隊列前面,優先出隊。
優先隊列具有隊列的所有特性,包括隊列的基本操作,只是在這基礎上添加了內部的一個排序,它本質是一個堆實現的。
和隊列相同的基本操作:
top 訪問隊頭元素
empty 隊列是否為空
size 返回隊列內元素個數
push 插入元素到隊尾 (並排序)
emplace 原地構造一個元素並插入隊列
pop 彈出隊頭元素
swap 交換內容
定義:
priority_queue<Type, Container, Functional>
Type 就是數據類型,Container 就是容器類型(Container必須是用數組實現的容器,比如vector,deque等等,但不能用 list。STL里面默認用的是vector),Functional 就是比較的方式。
當需要用自定義的數據類型時才需要傳入三個參數(因為此時需要重寫自己數據的Functional,也就是為自己的數據重載<(大頂堆)或>(小頂堆) ),使用基本數據類型時,只需要傳入數據類型,默認是大頂堆。
一般是:
//升序隊列,小頂堆
priority_queue <int,vector<int>,greater<int> > q; //降序隊列,大頂堆
priority_queue <int,vector<int>,less<int> >q; //greater和less是std實現的兩個仿函數(就是使一個類的使用看上去像一個函數。其實現就是類中實現一個operator(),這個類就有了類似函數的行為,就是一個仿函數類了)
1>基本類型優先隊列的例子:
#include<iostream> #include <queue>
using namespace std; int main() { //對於基礎類型 默認是大頂堆
priority_queue<int> a; //等同於 priority_queue<int, vector<int>, less<int> > a; // 這里一定要有空格,不然成了右移運算符↓↓
priority_queue<int, vector<int>, greater<int> > c; //這樣就是小頂堆
priority_queue<string> b; for (int i = 0; i < 5; i++) { a.push(i); c.push(i); } while (!a.empty()) { cout << a.top() << ' '; a.pop(); } cout << endl; while (!c.empty()) { cout << c.top() << ' '; c.pop(); } cout << endl; b.push("abc"); b.push("abcd"); b.push("cbd"); while (!b.empty()) { cout << b.top() << ' '; b.pop(); } cout << endl; return 0; }
運行結果:
4 3 2 1 0
0 1 2 3 4 cbd abcd abc 請按任意鍵繼續. . .
2>用pair做優先隊列元素的例子:
規則:pair的比較,先比較第一個元素,第一個相等比較第二個。
#include <iostream> #include <queue> #include <vector>
using namespace std; int main() { priority_queue<pair<int, int> > a; pair<int, int> b(1, 2); pair<int, int> c(1, 3); pair<int, int> d(2, 5); a.push(d); a.push(c); a.push(b); while (!a.empty()) { cout << a.top().first << ' ' << a.top().second << '\n'; a.pop(); } }
運行結果:
2 5
1 3
1 2 請按任意鍵繼續. . .
3>用自定義類型做優先隊列元素的例子
#include <iostream> #include <queue>
using namespace std; //方法1
struct tmp1 //運算符重載<
{ int x; tmp1(int a) {x = a;} bool operator<(const tmp1& a) const { return x < a.x; //大頂堆
} }; //方法2
struct tmp2 //重寫仿函數
{ bool operator() (tmp1 a, tmp1 b) { return a.x < b.x; //大頂堆
} }; int main() { tmp1 a(1); tmp1 b(2); tmp1 c(3); priority_queue<tmp1> d; d.push(b); d.push(c); d.push(a); while (!d.empty()) { cout << d.top().x << '\n'; d.pop(); } cout << endl; priority_queue<tmp1, vector<tmp1>, tmp2> f; f.push(b); f.push(c); f.push(a); while (!f.empty()) { cout << f.top().x << '\n'; f.pop(); } }
運行結果:
3
2
1
3
2
1 請按任意鍵繼續. . .
3>emplace方法
VS2017中的emplace源碼
1 template<class... _Valty>
2 void emplace(_Valty&&... _Val) 3 { // insert element at beginning
4 c.emplace_back(_STD forward<_Valty>(_Val)...); 5 _STD push_heap(c.begin(), c.end(), comp); 6 }
VS2017中的push源碼
1 void push(value_type&& _Val) 2 { // insert element at beginning
3 c.push_back(_STD move(_Val)); 4 _STD push_heap(c.begin(), c.end(), comp); 5 }
兩個方法的主要區別:在將新添加的元素堆中之前一個調用的是emplace_back()方法,一個調用的是push_back()方法。
下面主要分析emplace_back()方法和push_back()方法的區別:
c++開發中我們會經常用到插入操作對stl的各種容器進行操作,比如vector,map,set等。在引入右值引用,轉移構造函數,轉移復制運算符之前,通常使用push_back()向容器中加入一個右值元素(臨時對象)時,首先會調用構造函數構造這個臨時對象,然后需要調用拷貝構造函數將這個臨時對象放入容器中。原來的臨時變量釋放。這樣造成的問題就是臨時變量申請資源的浪費。
引入了右值引用,轉移構造函數后,push_back()右值時就會調用構造函數和轉移構造函數,如果可以在插入的時候直接構造,就只需要構造一次即可。這就是c++11 新加的emplace_back。
詳細區別請看:https://www.cnblogs.com/MrLiuZF/p/14071320.html
因此,優先隊的push方法和emplace方法在功能上並沒有很大的區別,只有實現上的細微區別。
現在測試一下:
由於STL優先隊列實際上就是有heap的方法實現的所以先測試heap中的push_back()和emplace_back(),這值得容器用vector實現。
使用push_heap將新元素入堆之前調用emplace_back():
1 #include <iostream>
2 #include <vector>
3 #include <algorithm>
4 #include <queue>
5
6 using namespace std; 7
8 void printHeap(vector<int> &v) { 9 for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) { 10 cout << *it << " "; 11 } 12 cout << "\n" << endl; 13 } 14
15 int main() 16 { 17 vector<int> min = { 10,30,22,6,15,9 }; 18
19 //建立小頂堆
20 make_heap(min.begin(), min.end(), greater<int>()); 21 printHeap(min);//6 10 9 30 15 22 22
23 //插入元素
24 min.emplace_back(20); 25 push_heap(min.begin(), min.end(), greater<int>());//該算法前提:必須在堆的條件下
26 printHeap(min); 27
28 return 0; 29 }
輸出:
使用push_heap將新元素入堆之前調用push_back():
1 #include <iostream>
2 #include <vector>
3 #include <algorithm>
4 #include <queue>
5
6 using namespace std; 7
8 void printHeap(vector<int> &v) { 9 for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) { 10 cout << *it << " "; 11 } 12 cout << "\n" << endl; 13 } 14
15 int main() 16 { 17 vector<int> min = { 10,30,22,6,15,9 }; 18
19 //建立小頂堆
20 make_heap(min.begin(), min.end(), greater<int>()); 21 printHeap(min);//6 10 9 30 15 22 22
23 //插入元素
24 min.push_back(20); 25 push_heap(min.begin(), min.end(), greater<int>());//該算法前提:必須在堆的條件下
26 printHeap(min); 27
28 return 0; 29 }
輸出:
現在直接使用priority_queue來測試emplace和push:
1 #include <iostream>
2 #include <vector>
3 #include <algorithm>
4 #include <queue>
5
6 using namespace std; 7
8 void printHeap(vector<int> &v) { 9 for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) { 10 cout << *it << " "; 11 } 12 cout << "\n" << endl; 13 } 14
15 int main() 16 { 17
18 priority_queue<int, vector<int>, less<int> > pq; 19 pq.push(6); 20 pq.push(15); 21 pq.push(9); 22 pq.push(10); 23 pq.push(30); 24 pq.push(22); 25
26 auto tpd = pq; 27 while (!tpd.empty()) { 28 cout << tpd.top() << " "; 29 tpd.pop(); 30 } 31 cout << endl; 32
33 auto tpd1 = pq; 34 tpd1.push(16); 35 while (!tpd1.empty()) { 36 cout << tpd1.top() << " "; 37 tpd1.pop(); 38 } 39 cout << endl; 40
41
42 auto tpd2 = pq; 43 tpd2.emplace(16); 44 while (!tpd2.empty()) { 45 cout << tpd2.top() << " "; 46 tpd2.pop(); 47 } 48 cout << endl; 49
50 return 0; 51 }
輸出: