大根堆

· · 个人记录

大根堆

定义方式:

大根堆的定义:

priority_queue<数据类型, 容器类型, 比较函数类型>队列名; //这种定义默认为大根堆

小根堆的定义:

利用greater<>作为比较函数

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

常用函数

push(x);//插入一个元素x
pop();//弹出队首元素 
top();//获取队首元素
size();//队列中元素的个数 

自定义数据类型:

方法一

struct node{ 
    string s; 
    int a;
    bool operator<(const node& x)const{ 
        return a<x.a;
    } 
}; 
priority_queue<node> n;

方法二

struct sss{ 
    tring s; 
    int x; 
};
struct cs{ 
    bool operator()(const sss& a,const sss& b)const{ 
        return a.x>b.x;
    }
}; 
priority_queue<sss,vector<sss>,cs> min_score_heap;