priority_queue
priority_queue 优先队列。其底层是用堆来实现的。队列为先进先出,而优先队列是将优先级最高的元素放在顶端,及优先队列中,队首元素一定是优先级最高的元素。
优先队列的头文件及声明
首先,你需要
#include <queue> //既然是队列那么必备
using namespace std;
其中,一个优先队列的基本格式是
priority_queue < Type > 队列名
比如
priority_queue <int> a;//默认越大的数据优先级高,及从大到小排列
priority_queue <double> b;
priority_queue <char> c;
另一种声明方式为
priority_queue <int, vector<int>, greater<int> > a;//越小的数据优先级越高及从小到大排序
代码演示
#include <iostream>
#include <queue>
using namespace std;
int main(void)
{
priority_queue <int> a;
for(int i = 0; i < 10; i++)
{
a.push(i);
}
cout << "a: ";
for(int i = 0; i < 10; i++)
{
cout << a.top() << ' ';
a.pop();
}
cout << endl;
priority_queue <int, vector<int>, greater<int> > b;
for(int i = 0; i < 10; i++)
{
b.push(i);
}
cout << "b: ";
for(int i = 0; i < 10; i++)
{
cout << b.top() << ' ';
b.pop();
}
return 0;
}
优先队列的基本操作
q.size();//返回队列中元素个数
q.top();//返回队列中第一个元素,及优先级最高的元素
q.pop();//删除队列中第一个元素
q.push(k);//将元素k放入队列并自动排序
q.empty();//判断队列是否为空,若为空则返回1, 若不为空则返回0
基本数据类型的优先级设置
此处指的基本数据类型为int,double, char等直接使用的数据类型,优先队列则默认值大的优先级高,即由大到小排列,string字符串则按字典序排列
priority_queue <int> a;
priority_queue <int, vector<int>, less<int> > a;
以上两种定义是等价的,其中第二个参数,是用来承载底层数据结构堆的容器默认使用vector, 第三个参数则为比较函数less表示数据大的优先级高,而greater表示数据小的优先级高
结构体的优先级设置
结构体类型需要自定义比较函数,分为两种方式
一.重载运算符
我们一般将重载运算符写在结构体内,所以不展示将重载运算符写在结构体外
重载运算符写在结构体中需要一个参数, 但是后面必须要加const修饰,否则报错,如
#include <iostream>
#include <queue>
using namespace std;
struct node{
int x;
char y;
bool operator < (const node &a) const
{
if(x > a.x) return 1;
else if(x == a.x)
{
if(y >= a.y) return 1;
else return 0;
}
else if(x < a.x) return 0;
}
};
int main(void)
{
priority_queue <node> q;
node k;
for(int i = 1, j = 'a'; i <= 10, j <= 'a' + 10; i++, j++)
{
k.x = i, k.y = j, q.push(k);
}
while(!q.empty())
{
cout << q.top().x << ' ' << q.top().y << endl;
q.pop();
}
return 0;
}
二.自定义比较函数
priority_queue中的三个参数,后两个可以省去,即为默认类型,如果油第三个参数则必定要写第二个参数。
#include <iostream>
#include <queue>
using namespace std;
struct node{
int x;
char y;
};
struct cmp //将结构体中按照x由小到大排列,若x相同,则按y由小到大排列
{
bool operator () (const node &a, const node &b) const
{
if(a.x == b.x) return a.y < b.y;
else return a.x < b.x;
}
};
int main(void)
{
priority_queue <node, vector<node>, cmp> q;
node k;
for(int i = 1, j = 'a'; i <= 10, j <= 'a' + 10; i++, j++)
{
k.x = i, k.y = j, q.push(k);
}
while(!q.empty())
{
cout << q.top().x << ' ' << q.top().y << endl;
q.pop();
}
return 0;
}
优先队列与sort函数比较及注意事项
sort函数有关详见:https://blog.csdn.net/zhbbbbbb/article/details/103455817
sort函数中默认为升序排列,而优先队列中默认为降序排列
而两者在自定义函数中的用法及其相似
如sort函数中 先对a升序排列,若a相同则对b降序排列,若b相同则对c降序排列
struct node{
int a;
int b;
double c;
}arr[105];
bool cmp(node x, node y)
{
if(x.a != y.a) return x.a < y.a;
if(x.b != y.b) return x.b > y.b;
return x.c > y.c;
}
sort(arr, arr+100, cmp);
在优先队列中
#include <iostream>
#include <queue>
using namespace std;
struct node{
int a;
int b;
double c;
}arr[105];
struct cmp
{
bool operator () (const node &x, const node &y) const
{
if(x.a != y.a) return x.a < y.a;
if(x.b != y.b) return x.b > y.b;
return x.c > y.c;
}
};
int main(void)
{
priority_queue <node, vector<node>, cmp> q;
node k;
k.a = 5, k.b = 10, k.c = 's',q.push(k);
k.a = 7, k.b = 14, k.c = 'a',q.push(k);
k.a = 5, k.b = 18, k.c = 'h',q.push(k);
k.a = 7, k.b = 14, k.c = 'k',q.push(k);
k.a = 23, k.b = 10, k.c = 's',q.push(k);
k.a = 1, k.b = 1, k.c = 'a',q.push(k);
while(!q.empty())
{
cout << q.top().a << ' ' << q.top().b << ' ' << q.top().c<< endl;
q.pop();
}
return 0;
}
a的ASCII码为97,以此类推
总结
以上就是优先队列入门级别,剩下的就靠你们去探索辣