How to clear/empty priority_queue of objects in C++

In this tutorial, I will show you how you can clear/empty priority_queue of objects in C++. Since there is no inbuilt function in priority_queue to clear/empty it, thus we will see other ways to do it.

Suppose we have a class:

class marks{
 public:
int maths,english;

marks(int maths, int english){  //to initialise values
this->maths = maths;
this->english = english;
}

};

and we have to print some queries. For this, we will be using the priority queue data structure and after printing, we then have to clear/erase the priority queue.

Clearing/erasing of priority_queue

Method 1

Poping the contents one by one.

while(!pq.empty()){
pq.pop();
}

Below is a code to print all the scores in descending order of maths score and then clearing the priority queue using this method:

#include <bits/stdc++.h>
using namespace std;
class marks{
 public:
    int maths,english;
    
    marks(int maths, int english){  //to initialise values
        this->maths = maths;
        this->english = english;
    }
};

bool operator<(const marks &student1 , const marks &student2)
{    //overloading operator< 
    return student1.maths < student2.maths; //descending order of maths score    
}

int main() {
    priority_queue<marks> pq;
    vector<pair<int,int>> vec={{60,75},{55,45},{90,65},{40,95}}; //pairs of scores 
    
    for(int i = 0 ;i < vec.size() ; i++){   
        pq.push(marks(vec[i].first,vec[i].second));//inserting in priority_queue 
    }
   
    while(!pq.empty()){        //check whether pq empty or not
        marks m = pq.top();
        pq.pop();                 //erasing content one by one
        cout<<m.maths<<' '<<m.english<<endl;
    }

    if(pq.empty())
        cout<<"Priority Queue cleared.";
  
  return 0;
}

Output:

90 65
60 75
55 45
40 95
Priority Queue cleared.

Method 2

Assigning a new empty priority queue to the original one.

priority_queue<marks> pq;
//print the query
pq = priority_queue<marks> (); // assign a new empty priority queue

Below is a code to print the highest score in English and then clearing the priority queue using this method:

#include <bits/stdc++.h>
using namespace std;
class marks{
 public:
    int maths,english;
    
    marks(int maths, int english){  //to initialise values
        this->maths = maths;
        this->english = english;
    }
};

bool operator<(const marks &student1 , const marks &student2)
{    //overloading operator< 
    return student1.english < student2.english; //descending order of english score    
}

int main() {
    priority_queue<marks> pq;
    vector<pair<int,int>> vec={{60,75},{55,45},{90,65},{40,95}}; //pairs of scores 
    
    for(int i = 0 ;i < vec.size() ; i++){
        pq.push(marks(vec[i].first,vec[i].second)); //inserting in priority_queue
    }
   
   marks m =pq.top();    //getting highest scorer in english
   cout<<"Highest score in english is "<<m.english<<endl;
   
   pq = priority_queue<marks> (); // clearing pq
   
   if(pq.empty())           //checking whether pq empty or not
   cout<<"Priority queue cleared.";
  
  return 0;
}

Output:

Highest score in english is 95
Priority queue cleared.

These were two of the methods through which we can clear/empty the priority queue in C++.

Leave a Reply

Your email address will not be published. Required fields are marked *