STL容器之优先队列
優先級隊列,以前刷題的時候用的比較熟,現在竟然我只能記得它的關鍵字是priority_queue(太傷了)。在一些定義了權重的地方這個數據結構是很有用的。
先回顧隊列的定義:隊列(queue)維護了一組對象,進入隊列的對象被放置在尾部,下一個被取出的元素則取自隊列的首部。priority_queue特別之處在于,允許用戶為隊列中存儲的元素設置優先級。這種隊列不是直接將新元素放置在隊列尾部,而是放在比它優先級低的元素前面。標準庫默認使用<操作符來確定對象之間的優先級關系,所以如果要使用自定義對象,需要重載 < 操作符。
優先隊列有兩種,一種是最大優先隊列;一種是最小優先隊列;每次取自隊列的第一個元素分別是優先級最大和優先級最小的元素。
1) 優先隊列的定義
包含頭文件:"queue.h", "functional.h"
可以使用具有默認優先級的已有數據結構;也可以再定義優先隊列的時候傳入自定義的優先級比較對象;或者使用自定義對象(數據結構),但是必須重載好< 操作符。?
2) 優先隊列的常用操作
| 優先級隊列支持的操作 |
| q.empty()???????? 如果隊列為空,則返回true,否則返回false q.size()????????????返回隊列中元素的個數 q.pop()???????????? 刪除隊首元素,但不返回其值 q.top()???????????? 返回具有最高優先級的元素值,但不刪除該元素 q.push(item)???? 在基于優先級的適當位置插入新元素 |
其中q.top()為查找操作,在最小優先隊列中搜索優先權最小的元素,在最大優先隊列中搜索優先權最大的元素。q.pop()為刪除該元素。優先隊列插入和刪除元素的復雜度都是O(lgn),所以很快
另外,在優先隊列中,元素可以具有相同的優先權。
下面這個C例子,包含了幾乎所有常見的優先隊列用法。?
#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 91 Queue 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 91 Queue 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 91 Queue 6:83 72 56 47 36 22 14 10 7 3 */
轉載于:https://www.cnblogs.com/lgh1992314/archive/2013/05/27/5835022.html
總結
以上是生活随笔為你收集整理的STL容器之优先队列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows API(一)
- 下一篇: JavaScript原型学习