优先队列---使用

因为sort只支持对可随机访问的容器进行排序,是不支持队列和栈的;所以这里介绍优先队列

优先队列功能:对数据自动排序

1、默认排序,从大到小

#include<queue>
priority_queue<int>p;
//数据输入的时候默认从大到小排序

2、从大到小排序-less

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

3、从小到大排序-greate

priority_queue <int,vector<int>,greater<int> > p;

结构体类型:重载大于号和小于号

struct node
{
    int id;
    int t;
    int next_t;
}temp;

bool operator < (node x,node y)//重载小于号,从小到大排序
{
    if(x.t==y.t)
        return x.id>y.id;
    else
        return x.t>y.t;
}

bool operator < (node x,node y)//重载大于号,从大到小排序
{
    if(x.t==y.t)
        return x.id<y.id;
    else
        return x.t<y.t;

}
priority_queue<node>p;

先构建cmp函数在重载

typedef struct node//定义结构体
{
    int num;//编号
    int cnt;//工作次数
    int time;
}node;

typedef struct cmp1//从某一权值从大到小排序
{
    bool operator () (const node &x, const node &y)
    {
        return x.time > y.time;
    }
}cmp1;
typedef struct cmp2//从小到大排序
{
    bool operator () (const node &x, const node &y)
    {
        return x.cnt < y.cnt;
    }

}cmp2;
priority_queue<node, vector<node>, cmp2>Q;
priority_queue<node, vector<node>, cmp1>T;

优先队列没有p.front()操作,返回队首第一个元素操作时用p.top();

https://blog.csdn.net/c20182030/article/details/70757660

原文地址:https://www.cnblogs.com/-citywall123/p/11285504.html