STL Priority Queue for Structure or Class

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 a example of declaration of struct Person:

filter_none

edit
close

play_arrow

link
brightness_4
code

struct Person{
int Age;
float Height;
}

chevron_right


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.



filter_none

edit
close

play_arrow

link
brightness_4
code

priority_queue<Person> pq;

chevron_right


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 structure below:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 overlading
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
    // CompareAge is the functor or comparision 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;
}

chevron_right


Output :
33 6.1
20 6
23 5.6
30 5.5
25 5

Given below is the implementation of priority_queue using Class

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output :
33 6.1
20 6
23 5.6
30 5.5
25 5


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


4


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.