原文:

优先级队列是的扩展,优先弹出与优先级相关联的元素和优先级较高的元素。

优先级队列可以包含具有各种数据类型的元素,例如整数、整数对、自定义数据类型。但是有一点很常见,那就是有一个元素定义了元素的优先级。 因此,优先级队列对可以有两种排序方式–

  • 按对的第一个元素排序
  • 按对的第二个元素排序

按第一个元素排序的优先级队列

在 c 中,如果元素是成对的形式,那么默认情况下,元素的优先级取决于第一个元素。因此,我们只需要使用成对的优先级队列。

c

// c   implementation of the
// priority queue of pairs
// ordered by the first element
#include 
#include 
using namespace std;
// function to print the data of
// the priority queue ordered by first
void showpq(
    priority_queue > g)
{
    // loop to print the elements
    // until the priority queue
    // is not empty
    while (!g.empty()) {
        cout << g.top().first
             << " " << g.top().second
             << endl;
        g.pop();
    }
    cout << endl;
}
// driver code
int main()
{
    priority_queue > p1;
    // insertion of elements
    p1.push(make_pair(4, 5));
    p1.push(make_pair(5, 4));
    p1.push(make_pair(1, 6));
    p1.push(make_pair(7, 3));
    p1.push(make_pair(9, 4));
    showpq(p1);
    return 0;
}

output:

9 4
7 3
5 4
4 5
1 6