优先队列的使用方法

FUWangsLaw 2018-05-13

//优先队列:个位数大的整数优先级小
//分析:优先级  个位数大 < 个位数小
priority_queue<int,vector<int>,cmp> 
struct cmp{
    bool operator()(const int a,const int b){
        return a%>b%;
    }
};
//效果相同,都是数字大的优先级高
priority_queue<int>;
priority_queue<int,vector<int>,less<int> >;
//数字小的优先级高 
priority_queue<int,vector<int>,greater<int> >;
结构体优先级设置
方法一: 
struct fruit {
    string name;
    int price;
    //价格高的优先级高 
    friend bool operator<(fruit f1,fruit f2)
    {
        return f1.price<f2.price; 
    }
}; 
或者
struct fruit{
    string name;
    int price;
    //价格低的优先级高 
    friend bool operator<(const fruit& f1,const fruit& f2)
    {
        return f1.price>f2.price;
    }
};
方法二:
struct fruit{
    string name;
    int price;
}; 
struct cmp{
    //价格高的优先级高 
    bool operator()(fruit f1,fruit f2)
    {
        return f1.price < f2.price;
    }
};
或者
struct fruit{
    string name;
    int price;
}; 
struct cmp{
    bool operator()(const fruit& f1,const fruit& f2)
    {
        return f1.price < f2.price;
    }
};

注意!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

优先队列的本质是堆!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

相关推荐