標籤:
隊列(queue)維護了一組對象,進入隊列的對象被放置在尾部,下一個被取出的元素則取自隊列的首部。
priority_queue特別之處在於,允許使用者為隊列中儲存的元素設定優先權。這種隊列不是直接將新元素放置在隊列尾部,而是放在比它優先順序低的元素前面。標準庫預設使用<操作符來確定對象之間的優先順序關係,所以如果要使用自訂對象,需要重載 < 操作符。每次從隊列中取出的是具有最高優先權的元素。優先隊列的效率:其時間複雜度為O(logn).n為隊列中元素的個數,存取都需要消耗時間。
1) 優先隊列的定義
包含標頭檔:#include<functional> #include<queue>
可以使用具有預設優先順序的已有資料結構;也可以再定義優先隊列的時候傳入自訂的優先順序比較對象;或者使用自訂對象(資料結構),但是必須重載好< 操作符。
2) 優先隊列的常用操作
| 優先順序隊列支援的操作 |
q.empty() 如果隊列為空白,則返回true,否則返回false q.size() 返回隊列中元素的個數 q.pop() 刪除隊首元素,但不返回其值 q.top() 返回具有最高優先順序的元素值,但不刪除該元素 q.push(item) 在基於優先順序的適當位置插入新元素 |
#include<iostream>#include<functional>#include<queue>#include<vector>using namespace std;//定義比較結構struct cmp1{ bool operator ()(int &a,int &b){ return a>b;//最小值優先 }};struct cmp2{ bool operator ()(int &a,int &b){ return a<b;//最大值優先 }};//自訂資料結構struct number1{ int x; bool operator < (const number1 &a) const { return x>a.x;//最小值優先 }};struct number2{ int x; bool operator < (const number2 &a) const { return x<a.x;//最大值優先 }};int a[]={14,10,56,7,83,22,36,91,3,47,72,0};number1 num1[]={14,10,56,7,83,22,36,91,3,47,72,0};number2 num2[]={14,10,56,7,83,22,36,91,3,47,72,0};int main(){ priority_queue<int>que;//採用預設優先順序構造隊列 priority_queue<int,vector<int>,cmp1>que1;//最小值優先 priority_queue<int,vector<int>,cmp2>que2;//最大值優先 priority_queue<int,vector<int>,greater<int> >que3;//注意“>>”會被認為錯誤, priority_queue<int,vector<int>,less<int> >que4;////最大值優先 priority_queue<number1>que5; //最小優先順序隊列 priority_queue<number2>que6; //最大優先順序隊列 int i; for(i=0;a[i];i++){ que.push(a[i]); que1.push(a[i]); que2.push(a[i]); que3.push(a[i]); que4.push(a[i]); } for(i=0;num1[i].x;i++) que5.push(num1[i]); for(i=0;num2[i].x;i++) que6.push(num2[i]); printf("採用預設優先關係:/n(priority_queue<int>que;)/n"); printf("Queue 0:/n"); while(!que.empty()){ printf("%3d",que.top()); que.pop(); } puts(""); puts(""); printf("採用結構體自訂優先順序方式一:/n(priority_queue<int,vector<int>,cmp>que;)/n"); printf("Queue 1:/n"); while(!que1.empty()){ printf("%3d",que1.top()); que1.pop(); } puts(""); printf("Queue 2:/n"); while(!que2.empty()){ printf("%3d",que2.top()); que2.pop(); } puts(""); puts(""); printf("採用標頭檔/"functional/"內定義優先順序:/n(priority_queue<int,vector<int>,greater<int>/less<int> >que;)/n"); printf("Queue 3:/n"); while(!que3.empty()){ printf("%3d",que3.top()); que3.pop(); } puts(""); printf("Queue 4:/n"); while(!que4.empty()){ printf("%3d",que4.top()); que4.pop(); } puts(""); puts(""); printf("採用結構體自訂優先順序方式二:/n(priority_queue<number>que)/n"); printf("Queue 5:/n"); while(!que5.empty()){ printf("%3d",que5.top()); que5.pop(); } puts(""); printf("Queue 6:/n"); while(!que6.empty()){ printf("%3d",que6.top()); que6.pop(); } puts(""); return 0;}/*運行結果 :採用預設優先關係:(priority_queue<int>que;)Queue 0:83 72 56 47 36 22 14 10 7 3採用結構體自訂優先順序方式一:(priority_queue<int,vector<int>,cmp>que;)Queue 1: 7 10 14 22 36 47 56 72 83 91Queue 2:83 72 56 47 36 22 14 10 7 3採用標頭檔"functional"內定義優先順序:(priority_queue<int,vector<int>,greater<int>/less<int> >que;)Queue 3: 7 10 14 22 36 47 56 72 83 91Queue 4:83 72 56 47 36 22 14 10 7 3採用結構體自訂優先順序方式二:(priority_queue<number>que)Queue 5: 7 10 14 22 36 47 56 72 83 91Queue 6:83 72 56 47 36 22 14 10 7 3*/
(轉加整理)優先隊列