priority_queue

priority_queue<int> p;//最大值优先,是大顶堆一种简写方式
priority_queue<int,vector<int>,greater<int>>q1;//最小值优先,小顶堆
priority_queue<int,vector<int>,less<int> >q2;//最大值优先,大顶堆

//其中第一个参数是数据类型,第二个参数为容器类型。第三个参数为比较函数。

struct node
{
    string name;
    int price;
    friend bool operator< (node a, node b)
    {
        return a.price < b.price; // 相当于less,这是大顶堆,反之则是小顶堆,最大值优先
    }
} stu; //定义结构体变量

这样直接可以:
priority_queue<node > q;

可以将比较运算符外置,方法如下
struct node
{
    string name;
    int price;
} stu; //定义结构体变量

struct cmp
{
    bool operator () (node a, node b) // 重载括号
    {
        return node.price < node.price; // 相当于less,大顶堆
    }
}

参考:http://www.cnblogs.com/aiguona/p/7200718.html

猜你喜欢

转载自www.cnblogs.com/tianzeng/p/9061014.html