优先队列

1.priority_queue

(1)基本类型

最大优先队列:

priority_queue<int> q;

priority_queue<int,vector<int> > q; 

priority_queue<int,vector<int>,less<int> > q;

最小优先队列:

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


http://www.cplusplus.com/reference/queue/priority_queue/


(2)自定义类型

重载:

//最大堆
struct Node
{
    int key;
};

bool operator<(Node a,Node b)
{
    return a.key<b.key;
}

priority_queue<Node> q;

仿函数:

//最大堆
struct Node
{
    int key;
};

struct cmp
{
    bool operator()(Node a,Node b)
    {
        return a.key<b.key;
    }
};

priority_queue<Node,vector<Node>,cmp> q;

//最小堆
struct Node
{
    int key;
};

struct cmp
{
    bool operator()(Node a,Node b)
    {
        return a.key>b.key;
    }
};

priority_queue<Node,vector<Node>,cmp> q;


2.heap

make_heap(),push_heap(),pop_heap(),sort_heap()


http://www.cplusplus.com/reference/algorithm/push_heap/




你可能感兴趣的:(优先队列)