Priority Queue of Sets in C++ with Examples
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.
Similar Reads
Priority Queue in C++ STL In C++, priority queue is a type of queue in which there is some priority assigned to the elements. According to this priority, elements are removed from the queue. By default, the value of the element being inserted is considered as priority. Higher its value, higher its priority. But this can be c
6 min read
priority_queue::push() and priority_queue::pop() in C++ STL In C++, priority_queue::push() and priority_queue::pop() methods are used to insert and delete the element from the priority_queue container. They both are the member functions of std::priority_queue class defined inside <queue> header file. In this article, we will learn about priority_queue:
2 min read
priority_queue::top() in C++ STL Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue. In general, elements are arranged according to some priority. However in C++ STL, the top element is the greatest elem
3 min read
priority_queue::empty() and priority_queue::size() in C++ STL Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue. However, in C++ STL (by default) the largest element is at the top. We can also create a priority queue having the sma
4 min read
priority_queue emplace() in C++ STL Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue. However, in C++ STL (by default) the largest element is at the top. We can also create a priority queue having the sma
4 min read
priority_queue::swap() in C++ STL Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue. However, in C++ STL (by default) the largest element is at the top. We can also create a priority queue having the sma
3 min read
priority_queue value_type in C++ STL The priority_queue :: value_type method is a built-in function in C++ STL which represents the type of object stored as an element in a priority_queue. It acts as a synonym for the template parameter. Time complexity: O(1)Syntax: priority_queue::value_type variable_name It has no parameters and no r
2 min read
Priority Queue of Sets in C++ with Examples 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}). Functi
4 min read
Priority queue of pairs in C++ with ordering by first and second element Priority Queue: Priority queue is the extension of the queue in which elements associated with priority and elements having higher priority is popped first. Priority queue can contain elements with various data types such as integer, pair of integers, custom data type. But one thing is common that t
5 min read
Multiple comparisons in a C++ priority queue? What is a Priority Queue? A Priority Queue is an abstract data type that is similar to a queue, and every element has some priority value associated with it. The priority of the elements in a priority queue determines the order in which elements are served (i.e., the order in which they are removed)
5 min read