优先队列

来源:互联网 发布:前端数据库 编辑:程序博客网 时间:2024/05/22 10:47

优先队列:普通队列具有先进先出的特点,优先队列出队时具有优先级别

#include<cstdio>
#include<queue>
using namespace std;
struct num1
{
    int time;
    bool operator < (const num1 &a)
    const
    {
        return time>a.time;             //按最小值优先
    }
};
struct num2
{
    int time;
    bool operator < (const num2 &a)
    const
    {
        return time<a.time;            // 按最大值优先
    }
};
int main()
{
    num2 b;
    int n;
    priority_queue<num2> mm;
    printf("please input n number\n");
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d",&b);
        mm.push(b);
    }
    while(!mm.empty())
    {
        num2 bb=mm.top();
        printf("%d ",bb.time);
        mm.pop();
    }
    return 0;
}
*/
/*#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
struct cmp1
{
    bool operator()(int &a,int &b)
    {
        return a>b;    //按最小值优先
    }
};
struct cmp2
{
    bool operator()(int &a,int &b)
    {
        return a<b;  //按最大值优先
    }
};
int main()
{
    priority_queue<int,vector<int>,cmp2> mm;
    int n,a;
    printf("please input n number\n");
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d",&a);
        mm.push(a);
    }
    while(!mm.empty())
    {
        int b=mm.top();
        printf("%d ",b);
        mm.pop();
    }
    return 0;
}
*/

0 0