Windows Message Queue((hdu1509))基础题

讲一下优先队列的用法,其实我就想用两种,至于重载运行符<则不是我想了解的。第一种是直接用默认的:priority_queue<int> Q;  这里表示优先队列Q里面的int型的数的优先级是根据数的值的大小。值越大,优先级越高。
第二种是自己写一个比较函数:
priority_queue<type,container,functional> Q;
type表示要进行比较的数据的类型,container则表示保存这个数据的容器,而functional则表示比较函数。
对于比较函数可以这样定义:
struct cmp
{
    bool operator()(type a,type b)
   {
            ..........;这里面的比较的方式与sort当中的比较函数写法一样,不过排序方式就有点不用。

    } 

对于自定义数据类型,要给出大小关系,即重载”<“ 运算符。
struct node{
 int x,y,step;
 bool operator <(const node &b) const  

  {
  return step>b.step;
 }
 friend bool operator<(node a,node b)

{
  return a.step>b.step;
 }
};

 

 

 

 

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct point
{
    int x,y,z;
    char a[100];
    friend bool operator <(point a,point b)
    {
        if(a.y!=b.y)
            return a.y>b.y;
        return a.x>b.x;
    }
};
int main()
{
    int m,n,i,j=1;
    priority_queue<point>q;
    point next;
    char b[100];
    while(scanf("%s",b)!=EOF)
    {
        if(strcmp(b,"GET")==0)
        {
       
            if(q.empty())
                printf("EMPTY QUEUE!\n");
            else
            {    next=q.top();
                q.pop();
                printf("%s %d\n",next.a,next.z);
            }
        }
        else
        {
            scanf("%s%d%d",next.a,&next.z,&next.y);
            next.x=j;
            j++;
            q.push(next);
        }
    }
    return 0;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值