头文件 : include
定义 : priority_queue
默认 : ==大顶堆==,比较方式默认用operator<
,所以如果把后面2个参数缺省的话,优先队列就是大顶堆(降序),队头元素最大
一般使用 :
// 注意 >>和 > >, c++11之后可以不用加空格
// 大顶堆
priority_queue, less > q;
// 小顶堆
priority_queue, greater > q;
//greater 和 less 是 std 实现的两个仿函数
//(就是使一个类的使用看上去像一个函数。其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了)
// pair 默认大顶堆,先比较第一个,再比较第二个;
priority_queue > q;
// 2 5
// 2 4
// 1 6
// 自定义优先级
// struct Node //运算符重载<
struct Node{
int x;
Node(int a) : x(a){}
bool operator<(const Node& a) const // 返回 true 时 a的优先级高
{
return x < a.x; //大顶堆
}
};