引入
優先隊列是一種特殊的隊列,在學習堆排序的時候就有所了解。
那么優先隊列是什么呢??
說白了,就是一種功能強大的隊列。
它的功能強大在哪里呢??
四個字:自動排序。
優先隊列的頭文件&&聲明
首先,你需要
#include<queue>
using namespace std;
這兩個頭文件。
其次,一個優先隊列聲明的基本格式是:?
priority_queue<結構類型> 隊列名;?
比如:
priority_queue <int> i;
priority_queue <double> d;
不過,我們最為常用的是這幾種:
priority_queue <node> q;
//node是一個結構體
//結構體里重載了‘<’小于符號
priority_queue <int,vector<int>,greater<int> > q;
//不需要#include<vector>頭文件
//注意后面兩個“>”不要寫在一起,“>>”是右移運算符
priority_queue <int,vector<int>,less<int> >q;
我們將在下文來講講這幾種聲明方式的不同。
優先隊列的基本操作
與隊列的基本操作如出一轍。?
如果想要了解請點擊這里,看關于隊列的介紹。
以一個名為q的優先隊列為例。
q.size();//返回q里元素個數
q.empty();//返回q是否為空,空則返回1,否則返回0
q.push(k);//在q的末尾插入k
q.pop();//刪掉q的第一個元素
q.top();//返回q的第一個元素
q.back();//返回q的末尾元素
優先隊列的特性
上文已經說過了,自動排序。?
怎么個排法呢??
在這里介紹一下:
默認的優先隊列(非結構體結構)
priority_queue <int> q;
這樣的優先隊列是怎樣的?讓我們寫程序驗證一下。
#include<cstdio>
#include<queue>
using namespace std;
priority_queue <int> q;
int main()
{q.push(10),q.push(8),q.push(12),q.push(14),q.push(6);while(!q.empty())printf("%d ",q.top()),q.pop();
}
程序大意就是在這個優先隊列里依次插入10、8、12、14、6,再輸出。?
結果是什么呢??
14 12 10 8 6?
也就是說,它是按從大到小排序的!
默認的優先隊列(結構體,重載小于)
先看看這個結構體是什么。
struct node
{int x,y;bool operator < (const node & a) const{return x<a.x;}
};
這個node結構體有兩個成員,x和y,它的小于規則是x小者小。?
再來看看驗證程序:
#include<cstdio>
#include<queue>
using namespace std;
struct node
{int x,y;bool operator < (const node & a) const{return x<a.x;}
}k;
priority_queue <node> q;
int main()
{k.x=10,k.y=100; q.push(k);k.x=12,k.y=60; q.push(k);k.x=14,k.y=40; q.push(k);k.x=6,k.y=80; q.push(k);k.x=8,k.y=20; q.push(k);while(!q.empty()){node m=q.top(); q.pop();printf("(%d,%d) ",m.x,m.y);}
}
程序大意就是插入(10,100),(12,60),(14,40),(6,20),(8,20)這五個node。?
再來看看它的輸出:?
(14,40) (12,60) (10,100) (8,20) (6,80)
它也是按照重載后的小于規則,從大到小排序的。
less和greater優先隊列
還是以int為例,先來聲明:
priority_queue <int,vector<int>,less<int> > p;
priority_queue <int,vector<int>,greater<int> > q;
話不多說,上程序和結果:
#include<cstdio>
#include<queue>
using namespace std;
priority_queue <int,vector<int>,less<int> > p;
priority_queue <int,vector<int>,greater<int> > q;
int a[5]={10,12,14,6,8};
int main()
{for(int i=0;i<5;i++)p.push(a[i]),q.push(a[i]);printf("less<int>:")while(!p.empty())printf("%d ",p.top()),p.pop(); ?pritntf("\ngreater<int>:")while(!q.empty())printf("%d ",q.top()),q.pop();
}
結果:?
less<int>:14 12 10 8 6?
greater<int>:6 8 10 12 14
所以,我們可以知道,less是從大到小,greater是從小到大。
作個總結
為了裝13方便,在平時,建議大家寫:
priority_queue<int,vector<int>,less<int> >q;
priority_queue<int,vector<int>,greater<int> >q;
平時如果用從大到小不用后面的vector<int>,less<int>,可能到時候要改成從小到大,你反而會搞忘怎么寫greater<int>,反而得不償失。
例子
?
Fence Repair
https://blog.csdn.net/weixin_43272781/article/details/83050793
?
?
總結
以上是生活随笔為你收集整理的priority_queue优先队列的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。