STL中的优先队列(堆)

来源:互联网 发布:网络安全法第27条规定 编辑:程序博客网 时间:2024/04/30 21:20

#include<queue>


priority_queue<数据类型,容器类型(默认vector),比较方式(less,greater)>  变量名

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

.front()返回堆首元素

.empty()判断是否为空

.push(x)将x压入堆

.pop()出堆操作


使用自定义类型时,需定义比较函数

struct cmp{    bool operator() ( Node a, Node b ){        if( a.x== b.x ) return a.y> b.y;                 return a.x> b.x; }    priority_queue<Node, vector, cmp> q;};

或者重载<运算符

bool operator<( Node a, Node b ){    if( a.x== b.x ) return a.y> b.y;    return a.x> b.x;     priority_queue<Node> q;}



原创粉丝点击