STL中優先隊列用法
阿新 • • 發佈:2019-04-12
編譯錯誤 雙向 clu 方式 https iter deque 類型 www. priority_queue<vector<int>, less<int> > pq1; // 使用遞增less<int>函數對象排序
priority_queue<deque<int>, greater<int> > pq2; // 使用遞減greater<int>函數對象排序
其成員函數有“判空(empty)” 、“尺寸(Size)” 、“棧頂元素(top)” 、“壓棧(push)” 、“彈棧(pop)”等。
struct cmp {
operator bool ()(int x, int y)
{
return x > y; // x小的優先級高 //也可以寫成其他方式,如: return p[x] > p[y];表示p[i]小的優先級高
}
};
priority_queue<int, vector<int>, cmp> q; //定義方法
//其中,第二個參數為容器類型。第三個參數為比較函數。
一、相關定義
優先隊列容器與隊列一樣,只能從隊尾插入元素,從隊首刪除元素。但是它有一個特性,就是隊列中最大的元素總是位於隊首,所以出隊時,並非按照先進先出的原則進行,而是將當前隊列中最大的元素出隊。這點類似於給隊列裏的元素進行了由大到小的順序排序。元素的比較規則默認按元素值由大到小排序,可以重載“<”操作符來重新定義比較規則。
優先級隊列可以用向量(vector)或雙向隊列(deque)來實現(註意list container不能用來實現queue,因為list的叠代器不是任意存取iterator,而pop中用到堆排序時是要求randomaccess iterator 的!):
priority_queue<deque<int>, greater<int> > pq2; // 使用遞減greater<int>函數對象排序
其成員函數有“判空(empty)” 、“尺寸(Size)” 、“棧頂元素(top)” 、“壓棧(push)” 、“彈棧(pop)”等。
二、priority_queue
基本操作:
empty() 如果隊列為空,則返回真
pop() 刪除對頂元素,刪除第一個元素
push() 加入一個元素
size() 返回優先隊列中擁有的元素個數
top() 返回優先隊列對頂元素,返回優先隊列中有最高優先級的元素
在默認的優先隊列中,優先級高的先出隊。在默認的int型中先出隊的為較大的數。
頭文件:
#include <queue>聲明方式:
1、普通方法:
priority_queue<int> q; //通過操作,按照元素從大到小的順序出隊 priority_queue<int,vector<int>, greater<int> > q; //通過操作,按照元素從小到大的順序出隊2、自定義優先級:
3、結構體聲明方式:
struct node { int x, y; friend bool operator < (node a, node b) { return a.x > b.x; //結構體中,x小的優先級高 } }; priority_queue<node>q; //定義方法 //在該結構中,y為值, x為優先級。 //通過自定義operator<操作符來比較元素中的優先級。 //在重載”<”時,最好不要重載”>”,可能會發生編譯錯誤 三、代碼實現優先隊列,其構造及具體實現我們可以先不用深究,我們現在只需要了解其特性,及在做題中的用法。
以一個例子來解釋吧(呃,寫完才發現,這個代碼包函了幾乎所有我們要用到的用法,仔細看看吧):
1 /*優先隊列的基本使用 2017/8/1 xzxl*/ 2 #include<stdio.h> 3 #include<functional> 4 #include<queue> 5 #include<vector> 6 using namespace std; 7 //定義結構,使用運算符重載,自定義優先級1 8 struct cmp1{ 9 bool operator ()(int &a,int &b){ 10 return a>b;//最小值優先 11 } 12 }; 13 struct cmp2{ 14 bool operator ()(int &a,int &b){ 15 return a<b;//最大值優先 16 } 17 }; 18 //定義結構,使用運算符重載,自定義優先級2 19 struct number1{ 20 int x; 21 bool operator < (const number1 &a) const { 22 return x>a.x;//最小值優先 23 } 24 }; 25 struct number2{ 26 int x; 27 bool operator < (const number2 &a) const { 28 return x<a.x;//最大值優先 29 } 30 }; 31 int a[]={14,10,56,7,83,22,36,91,3,47,72,0}; 32 number1 num1[]={14,10,56,7,83,22,36,91,3,47,72,0}; 33 number2 num2[]={14,10,56,7,83,22,36,91,3,47,72,0}; 34 35 int main() 36 { priority_queue<int>que;//采用默認優先級構造隊列 37 38 priority_queue<int,vector<int>,cmp1>que1;//最小值優先 39 priority_queue<int,vector<int>,cmp2>que2;//最大值優先 40 41 priority_queue<int,vector<int>,greater<int> >que3;//註意“>>”會被認為錯誤, 42 //這是右移運算符,所以這裏用空格號隔開 43 priority_queue<int,vector<int>,less<int> >que4;////最大值優先 44 45 priority_queue<number1>que5; 46 priority_queue<number2>que6; 47 48 int i; 49 for(i=0;a[i];i++){ 50 que.push(a[i]); 51 que1.push(a[i]); 52 que2.push(a[i]); 53 que3.push(a[i]); 54 que4.push(a[i]); 55 } 56 for(i=0;num1[i].x;i++) 57 que5.push(num1[i]); 58 for(i=0;num2[i].x;i++) 59 que6.push(num2[i]); 60 61 62 printf("采用默認優先關系:\n(priority_queue<int>que;)\n"); 63 printf("Queue 0:\n"); 64 while(!que.empty()){ 65 printf("%3d",que.top()); 66 que.pop(); 67 } 68 puts(""); 69 puts(""); 70 71 printf("采用結構體自定義優先級方式一:\n(priority_queue<int,vector<int>,cmp>que;)\n"); 72 printf("Queue 1:\n"); 73 while(!que1.empty()){ 74 printf("%3d",que1.top()); 75 que1.pop(); 76 } 77 puts(""); 78 printf("Queue 2:\n"); 79 while(!que2.empty()){ 80 printf("%3d",que2.top()); 81 que2.pop(); 82 } 83 puts(""); 84 puts(""); 85 printf("采用頭文件\"functional\"內定義優先級:\n(priority_queue<int,vector<int>,greater<int>/less<int> >que;)\n"); 86 printf("Queue 3:\n"); 87 while(!que3.empty()){ 88 printf("%3d",que3.top()); 89 que3.pop(); 90 } 91 puts(""); 92 printf("Queue 4:\n"); 93 while(!que4.empty()){ 94 printf("%3d",que4.top()); 95 que4.pop(); 96 } 97 puts(""); 98 puts(""); 99 printf("采用結構體自定義優先級方式二:\n(priority_queue<number>que)\n"); 100 printf("Queue 5:\n"); 101 while(!que5.empty()){ 102 printf("%3d",que5.top()); 103 que5.pop(); 104 } 105 puts(""); 106 printf("Queue 6:\n"); 107 while(!que6.empty()){ 108 printf("%3d",que6.top()); 109 que6.pop(); 110 } 111 puts(""); 112 return 0; 113 } 114 /* 115 運行結果 : 116 采用默認優先關系: 117 (priority_queue<int>que;) 118 Queue 0: 119 83 72 56 47 36 22 14 10 7 3 120 121 采用結構體自定義優先級方式一: 122 (priority_queue<int,vector<int>,cmp>que;) 123 Queue 1: 124 7 10 14 22 36 47 56 72 83 91 125 Queue 2: 126 83 72 56 47 36 22 14 10 7 3 127 128 采用頭文件"functional"內定義優先級: 129 (priority_queue<int,vector<int>,greater<int>/less<int> >que;) 130 Queue 3: 131 7 10 14 22 36 47 56 72 83 91 132 Queue 4: 133 83 72 56 47 36 22 14 10 7 3 134 135 采用結構體自定義優先級方式二: 136 (priority_queue<number>que) 137 Queue 5: 138 7 10 14 22 36 47 56 72 83 91 139 Queue 6: 140 83 72 56 47 36 22 14 10 7 3 141 */
文章來源:https://www.cnblogs.com/xzxl/p/7266404.html
如果覺得實用,請點擊“好文要頂”並“支持”。
STL中優先隊列用法