原文:

在 c 中, 实现。以下是创建类型的优先级队列的一些示例。

按第一个元素排序的最大优先级队列(或最大堆)

// c   program to create a priority queue of pairs.
// by default a max heap is created ordered
// by first element of pair.
#include 
using namespace std;
// driver program to test methods of graph class
int main()
{
    // by default a max heap is created ordered
    // by first element of pair.
    priority_queue > pq;
    pq.push(make_pair(10, 200));
    pq.push(make_pair(20, 100));
    pq.push(make_pair(15, 400));
    pair top = pq.top();
    cout << top.first << " " << top.second;
    return 0;
}

输出:

20 100

按第一个元素排序的最小优先级队列(或最小堆)

// c   program to create a priority queue of pairs.
// we can create a min heap by passing adding two 
// parameters, vector and greater().
#include 
using namespace std;
typedef pair pi;
// driver program to test methods of graph class
int main()
{
    // by default a min heap is created ordered
    // by first element of pair.
    priority_queue, greater > pq;
    pq.push(make_pair(10, 200));
    pq.push(make_pair(20, 100));
    pq.push(make_pair(15, 400));
    pair top = pq.top();
    cout << top.first << " " << top.second;
    return 0;
}

输出:

10 200