stl-优先队列

本文介绍了一个使用C++实现的优先队列应用实例。通过定义一个包含比较运算符的结构体,并利用优先队列的基本操作如push、pop、top等,演示了如何处理具有优先级的数据集合。该实例特别关注于基于元素属性进行排序的需求。

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

优先队列

头文件#include<queue>

函数:push()进队,
pop()出队,
size()返回元素个数,
top()返回最高优先级的元素,
empty()队是否为空

#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
struct node{
	int x;
	int y;
	    friend bool operator < (node a, node b)
    {
        return a.x > b.x;//结构体中,x小的优先级高
    }
};
int main()
{
	//priority_queue<int,vector<int>, greater<int> > q; 从小到大pop 
    //priority_queue<int> q;从大到小pop 
    priority_queue<node> q;
	int n,a,w;
	node nod;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&nod.x);
		scanf("%d",&nod.y);
		q.push(nod);
	   }   
	  	printf("%4d%4d",q.top().x,q.top().y);//输出节点中x最小的结点信息 
	   	q.pop();
       return 0; 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值