Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Priority Queue of Sets in C++ with Examples

  • Difficulty Level : Medium
  • Last Updated : 18 Jan, 2022

Priority Queues

Priority queues are a type of container adapters, specifically designed such that the first element of the queue is the greatest of all elements in the queue and elements are in nonincreasing order (hence we can see that each element of the queue has a priority {fixed order}).

Functions used with Priority Queues:

  • empty(): Returns whether the queue is empty.
  • size(): Returns the size of the queue.
  • top(): Returns a reference to the topmost element of the queue.
  • pop(): Deletes the first element of the queue.

Sets

Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. 

Functions used with Sets:

  • size(): Returns the number of elements in the set.
  • insert(const x): Adds a new element ‘x’ to the set.

Priority queue of sets can be quite useful for designing complex data structures.

Syntax :

priority_queue<set<data_type>> priorityQueue 
This stores set as an element in the max-heap priority queue

Below is the implementation of the max-heap priority queue of set:

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print priority
// queue contents
void print(priority_queue<set<int> > priorityQueue)
{
    while (!priorityQueue.empty()) {
        // Each element of the priority
        // queue is a set itself
        set<int> st = priorityQueue.top();
 
        cout << "[ ";
 
        // Print the set elements
        for (auto element : st)
            cout << element << ' ';
        cout << ']';
        cout << '\n';
 
        // Pop out the topmost set
        priorityQueue.pop();
    }
}
 
// Driver code
int main()
{
    // Declaring a max-heap priority
    // queue
    priority_queue<set<int> > priorityQueue;
 
    // Declaring a set of integers
    set<int> set1;
 
    // Inserting into the set
    set1.insert(10);
    set1.insert(1);
    set1.insert(2);
    set1.insert(5);
 
    // Push the set into priority
    // queue
    priorityQueue.push(set1);
 
    // Declaring another set
    set<int> set2;
 
    // Inserting into the set
    set2.insert(2);
    set2.insert(7);
    set2.insert(12);
    set2.insert(1);
 
    // Push the set into priority queue
    priorityQueue.push(set2);
 
    // Declaring another set
    set<int> set3;
 
    // Inserting into the set
    set3.insert(4);
    set3.insert(7);
    set3.insert(12);
    set3.insert(13);
 
    // Push the set into priority queue
    priorityQueue.push(set3);
 
    // Print the priority queue
    print(priorityQueue);
 
    return 0;
}

 
 

Output
[ 4 7 12 13 ]
[ 1 2 7 12 ]
[ 1 2 5 10 ]

 

By default priority queue is a max-heap, therefore internally for two sets inside the priority queue, the set having a greater first element is the topmost element. If the first element is equal then the second value of sets is compared and so on.



 

Syntax:

 

priority_queue<set<data_type>, vector<set<data_type>>, greater<set<data_type>>> priorityQueue 
This stores set as an element in the min-heap priority queue

 

Below is the implementation of the min-heap priority queue of set:

 

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print priority
// queue contents
void print(priority_queue<set<int>,
                          vector<set<int> >,
                          greater<set<int> > >
               priorityQueue)
{
    while (!priorityQueue.empty()) {
        // Each element of the priority
        // queue is a set itself
        set<int> st = priorityQueue.top();
 
        cout << "[ ";
 
        // Print the set elements
        for (auto element : st)
            cout << element << ' ';
 
        cout << ']';
        cout << '\n';
 
        // Pop out the topmost set
        priorityQueue.pop();
    }
}
 
// Driver code
int main()
{
    // Declaring a min-heap
    // priority queue
    priority_queue<set<int>,
                   vector<set<int> >,
                   greater<set<int> > >
        priorityQueue;
 
    // Declaring a set of integers
    set<int> set1;
 
    // Inserting into the set
    set1.insert(10);
    set1.insert(1);
    set1.insert(2);
    set1.insert(5);
 
    // Push the set into priority
    // queue
    priorityQueue.push(set1);
 
    // Declaring another set
    set<int> set2;
 
    // Inserting into the set
    set2.insert(2);
    set2.insert(7);
    set2.insert(12);
    set2.insert(1);
 
    // Push the set into priority
    // queue
    priorityQueue.push(set2);
 
    // Declaring another set
    set<int> set3;
 
    // Inserting into the set
    set3.insert(4);
    set3.insert(7);
    set3.insert(12);
    set3.insert(13);
 
    // Push the set into priority
    // queue
    priorityQueue.push(set3);
 
    // Print the priority queue
    print(priorityQueue);
 
    return 0;
}

 
 

Output
[ 1 2 5 10 ]
[ 1 2 7 12 ]
[ 4 7 12 13 ]

 

Internally for two sets inside the min-heap priority queue, the set having the smaller first element is the topmost element. If the first element is equal then the second value of sets is compared and so on.

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!