STL——priority_queue详细用法

本文深入讲解了优先队列(priority_queue)的使用方法,包括其底层实现、常见操作如push、top、pop及empty和size函数,探讨了如何设置元素优先级,并提供了基本数据结构和自定义结构体的示例,最后讨论了优先队列在算法优化中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

priority_queue常用方法讲解

priority_queue又被称为优先队列,底层用堆实现队首元素一定是队内元素中优先级最高的,
当然可以在任意时刻在堆中添加元素,优先队列会随时进行调整结构,使得每次出队的元素的优先级最大
这里的优先级是规定出来的,可以从小到大也可以从大到小

priority_queue的定义

要使用优先队列要先加入头文件并且定义方法与其他容器基本相同

#include<queue>
using namespace std;
priority_queue< 定义类型 > name;
2.priority_queue容器内元素访问

和队列不一样的是优先队列没有front()和back()函数,只能通过top()来访问队首元素其他地方也会叫它堆顶元素,实例:

#include<iostream>
#include<queue>
using namespace std;
int main()
{
	priority_queue<int> q;
	q.push(3);
	q.push(1);
	q.push(4);
	cout<<q.top()<<endl;
	return 0;
} 
//结果为  4
3.priority_queue常用函数
(1)push()

push(x)是将x入队,时间复杂度为O(logN),N为容器内元素个数。

(2)top()

top()可以获得队首元素时间复杂度为O(logN)

(3)pop()

pop()将队首元素出队,时间复杂度为(logN)

#include<iostream>
#include<queue>
using namespace std;
int main()
{
	priority_queue<int> q;
	q.push(3);
	q.push(1);
	q.push(4);
	cout<<q.top()<<endl;
	q.pop();
	cout<<q.top()<<endl;
	return 0;
} 
输出  4   3
(4)empty()

检测队列是否非空与queue相同
时间复杂度为O(1)
空 -> 真
非空 -> 假

(5)size()

返回优先队列的元素个数时间复杂度为O(1)

4.priority_queue()内元素的优先级设置

如何定义队列内优先级是运用优先级的关键

(1)基本数据结构的优先级设置

此处的类型就是,int,double,char…可以直接被使用的数据类型,一般来说是数字大的优先级高,char则是按字典序最大的,对基本定义类型来说,这两种是等价的

priority_queue<int> q;
priority_queue<int,vector<int>,less<int> >

可以发现第二种定义多了两个参数,第二个参数vector是承载堆的容器,而第三个参数less是表示数字大的优先级大,而greater是指数字越小优先级越大
可以做一个实例

#include<iostream>
#include<queue>
using namespace std;
int main()
{
	priority_queue<int,vector<int>,greater<int> > q;
	q.push(3);
	q.push(1);
	q.push(4);
	cout<<q.top()<<endl;
	return 0;
} 
//结果为  1
(2)结构体的优先级设置

先建立一个成绩单的结构体

struct student
{
  string name;
  int score;
};

现在希望可以按学生分数从高到低排名,需要重载小于号(<),重载就是对已有的运算符进行重新定义,先写下来:

struct student
{
  string name;
  int score;
  friend bool operator < (student s1,student s2){
      return s1.score < s2.score;
  }
};

student结构体中加入了一个函数,其中friend为友元,后面的bool operator < (student s1,student s2)是对运算符“<”(重载大于号会编译错误,因为数学上来说之需要重载小于号)
函数内部为return s1.score < s2.score,所以说重载后小于号还是小于号的作用,此时就可以直接定义student的结构体内部就是分数高的学生优先级高,如果要确定分数低的优先级高如下所示:

struct student
{
  string name;
  int score;
  friend bool operator < (student s1,student s2){
      return s1.score > s2.score;
  }
};

最后如果出现了结构体内数据较为庞大,建议使用引用来提高效率,只需要加上const 和&,例如

struct student
{
  string name;
  int score;
  friend bool operator < (const student &s1,const student &s2){
      return s1.score > s2.score;
  }
};
5.priority_queue的用途

可以解决一些贪心问题,也可以对dijkstra算法进行优化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数学小牛马

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值