Skip to content
Related Articles

Related Articles

STL Priority Queue for Structure or Class
  • Difficulty Level : Medium
  • Last Updated : 03 Feb, 2021
GeeksforGeeks - Summer Carnival Banner

STL priority_queue is the implementation of Heap Data-structure. By default, it’s a max heap and we can easily use it for primitive datatypes. There are some important applications of it which can be found here

Prerequisite: Prioirty_queue Basics

In this article, we will see how can we use priority_queue for custom datatypes like class or structure. 
suppose we have a structure name Person which consist of two variables Age and height 
and we want to store that that in priority_queue then a simple method won’t work here.

Given below is an example of the declaration of struct Person: 

C++




struct Person{
int Age;
float Height;
}

On defining the Priority Queue as shown below, it’ll give us error since priority_queue doesn’t know on what order(min or max) we need to arrange the objects.



C++




priority_queue<Person> pq;

To rectify the error above, we will use operator overloading to define the priority. So that priority_queue can decide how to store the structure object.

Given below is the priority_queue implementation with the structure below: 

C++




// program in c++ to use priority_queue with structure
 
#include <iostream>
#include <queue>
using namespace std;
#define ROW 5
#define COL 2
 
struct Person {
 
    int age;
 
    float height;
 
    // this will used to initialize the variables
    // of the structure
    Person(int age, float height)
        : age(age), height(height)
    {
    }
};
 
// this is an strucure which implements the
// operator overloading
struct CompareHeight {
    bool operator()(Person const& p1, Person const& p2)
    {
        // return "true" if "p1" is ordered
        // before "p2", for example:
        return p1.height < p2.height;
    }
};
 
int main()
{
    priority_queue<Person, vector<Person>, CompareHeight> Q;
 
    // When we use priority_queue with  structure
    // then we need this kind of syntax where
    // CompareHeight is the functor or comparison function
    float arr[ROW][COL] = { { 30, 5.5 }, { 25, 5 },
                    { 20, 6 }, { 33, 6.1 }, { 23, 5.6 } };
 
    for (int i = 0; i < ROW; ++i) {
 
        Q.push(Person(arr[i][0], arr[i][1]));
 
        // insert an object in priority_queue by using
        // the Person strucure constructor
    }
 
    while (!Q.empty()) {
        Person p = Q.top();
        Q.pop();
        cout << p.age << " " << p.height << "\n";
    }
    return 0;
}

Output :

33 6.1
20 6
23 5.6
30 5.5
25 5

Given below is the implementation of priority_queue using Class 

C++




// program in c++ to use priority_queue with class
#include <iostream>
#include <queue>
using namespace std;
 
#define ROW 5
#define COL 2
 
class Person {
 
public:
    int age;
 
    float height;
 
    // this is used to initialize the variables of the class
    Person(int age, float height)
        : age(age), height(height)
    {
    }
};
 
// we are doing operator overloading through this
bool operator<(const Person& p1, const Person& p2)
{
 
    // this will return true when second person
    // has greater height. Suppose we have p1.height=5
    // and p2.height=5.5 then the object which
    // have max height will be at the top(or
    // max priority)
    return p1.height < p2.height;
}
 
int main()
{
 
    priority_queue<Person> Q;
 
    float arr[ROW][COL] = { { 30, 5.5 }, { 25, 5 },
               { 20, 6 }, { 33, 6.1 }, { 23, 5.6 } };
 
    for (int i = 0; i < ROW; ++i) {
 
        Q.push(Person(arr[i][0], arr[i][1]));
 
        // insert an object in priority_queue by using
        // the Person class constructor
    }
 
    while (!Q.empty()) {
 
        Person p = Q.top();
 
        Q.pop();
 
        cout << p.age << " " << p.height << "\n";
    }
    return 0;
}

Output :

33 6.1
20 6
23 5.6
30 5.5
25 5
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :