Open In App

Priority Queue of Tuples in C++ with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

Priority Queue

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 associated with a Priority Queue:

  • empty(): This function returns whether the queue is empty.
  • size(): This function returns the size of the queue.
  • top(): Returns a reference to the topmost element of the queue.
  • push(x): This function adds the element ‘x’ at the end of the queue.

Tuple

A tuple is an object that can hold a number of elements. The elements can be of different data types. The elements of tuples are initialized as arguments in the order in which they will be accessed.

Functions associated with a tuple:

  • get(): get() is used to access the tuple values and modify them, it accepts the index and tuple name as arguments to access a particular tuple element.
  • make_tuple(): make_tuple() is used to assign tuple with values. The values passed should be in order with the values declared in the tuple.

This article focuses on how the priority queue of tuples can be used in C++. Priority queue of tuples can be quite useful while designing complex data structures.

Syntax 1: Max-heap priority queue of tuples:
priority_queue<tuple<data_type1, data_type2, data_type3>> priorityQueue;

Syntax 2: Min-heap priority queue of tuples:
priority_queue<tuple<data_type1, data_type2, data_type3>, 
vector<tuple<data_type1, data_type2, data_type3>>, 
greater<tuple<data_type1, data_type2, data_type3>>> priorityQueue;

Syntax 3: Customized priority queue of tuples (using comparator):
priority_queue<tuple<data_type1, data_type2, data_type3>, 
vector<tuple<data_type1, data_type2, data_type3>>, comparator> priorityQueue;

Max-heap priority queue of tuples:

By default priority queues are max-heap. So, In the case of a pair of tuples in the priority queue, their first element is compared, and if the first element of both tuples is equal then their second element is compared and if the second element is also equal then the third element is compared. The tuple having the larger element becomes the topmost element of the priority queue.  

Example 1: Below is the C++ program to implement max-heap priority queues of tuples.

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to print priority queue
// contents. Deliberately passing it
// call by value since we don't want
// to remove elements from the priority 
// queue
void print(priority_queue<tuple<int
           int, char> > priorityQueue)
{
  while (!priorityQueue.empty()) 
  {
    // Each element of the priority
    // queue is a tuple itself
    tuple<int, int, char> Tuple = 
          priorityQueue.top();
  
    cout << "[ ";
  
    cout << get<0>(Tuple) << ' ' << 
            get<1>(Tuple) << ' ' << 
            get<2>(Tuple);
    cout << ']';
    cout << '\n';
  
    // Pop out the topmost tuple
    priorityQueue.pop();
  }
}
  
// Driver code
int main()
{
  // Declaring a priority queue of
  // tuple
  priority_queue<tuple<int, int
  char> > priorityQueue;
  
  // Declaring a tuple
  tuple<int, int, char> tuple1;
  
  // Initializing the tuple
  tuple1 = make_tuple(1, 1, 'G');
  
  // Pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple1);
  
  // Declaring another tuple
  tuple<int, int, char> tuple2;
  
  // Initializing the tuple
  tuple2 = make_tuple(2, 2, 'e');
  
  // Pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple2);
  
  // Declaring another tuple
  tuple<int, int, char> tuple3;
  
  // Initializing the tuple
  tuple3 = make_tuple(3, 3, 'e');
  
  // Pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple3);
  
  // Declaring another tuple
  tuple<int, int, char> tuple4;
  
  // Initializing the tuple
  tuple4 = make_tuple(4, 4, 'k');
  
  // Pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple4);
  
  // Calling print function
  print(priorityQueue);
  
  return 0;
}


Output

[ 4 4 k]
[ 3 3 e]
[ 2 2 e]
[ 1 1 G]

Example 2: Below is the C++ program to demonstrate the working of the max-heap priority queue of tuples.

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to print priority queue
// contents. Deliberately passing it
// call by value since we don't want
// to remove elements from the priority 
// queue
void print(priority_queue<tuple<int, int
           string> > priorityQueue)
{
  while (!priorityQueue.empty()) 
  {
    // Each element of the priority
    // queue is a tuple itself
    tuple<int, int, string> Tuple = 
          priorityQueue.top();
  
    cout << "[ ";
  
    cout << get<0>(Tuple) << ' ' << 
            get<1>(Tuple) << ' ' << 
            get<2>(Tuple);
    cout << ']';
    cout << '\n';
  
    // Pop out the topmost tuple
    priorityQueue.pop();
  }
}
  
// Driver code
int main()
{
  // Declaring a priority queue
  // of tuple
  priority_queue<tuple<int, int
  string> > priorityQueue;
  
  // Declaring a tuple
  tuple<int, int, string> tuple1;
  
  // Initializing the tuple
  tuple1 = make_tuple(0, 0, "Geeks");
  
  // Pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple1);
  
  // Declaring a tuple
  tuple<int, int, string> tuple2;
  
  // Initializing the tuple
  tuple2 = make_tuple(0, 0, 
                      "Programming");
  
  // Pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple2);
  
  // Declaring a tuple
  tuple<int, int, string> tuple3;
  
  // Initializing the tuple
  tuple3 = make_tuple(1, 2, 
                      "Geeks");
  
  // Pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple3);
  
  // Declaring a tuple
  tuple<int, int, string> tuple4;
  
  // Initializing the tuple
  tuple4 = make_tuple(1, 3, 
                      "Programming");
  
  // Pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple4);
  
  // Calling print function
  print(priorityQueue);
  
  return 0;
}


Output

[ 1 3 Programming]
[ 1 2 Geeks]
[ 0 0 Programming]
[ 0 0 Geeks]

Min-heap priority queue of tuples:

In the case of a pair of tuples in the min-heap priority queue, their first element is compared, and if the first element of both tuples is equal then their second element is compared and if the second element is also equal then the third element is compared. The tuple having the smaller element becomes the topmost element of the priority queue.  

Example 1: Below is the C++ program to implement the working of the min-heap priority queue of tuples.

C++




// C++ program to implement 
// the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to print priority queue
// contents. Deliberately passing it
// call by value since we don't want
// to remove elements from the priority 
// queue
void print(priority_queue<tuple<int, int, char>,
           vector<tuple<int, int, char> >,
           greater<tuple<int, int, char> > >
           priorityQueue)
{
  while (!priorityQueue.empty()) 
  {
    // Each element of the priority
    // queue is a tuple itself
    tuple<int, int, char> Tuple = 
          priorityQueue.top();
  
    cout << "[ ";
  
    cout << get<0>(Tuple) << ' ' << 
            get<1>(Tuple) << ' ' << 
            get<2>(Tuple);
    cout << ']';
    cout << '\n';
  
    // Pop out the topmost tuple
    priorityQueue.pop();
  }
}
  
// Driver code
int main()
{
  // Declaring a min-heap priority 
  // queue of tuple
  priority_queue<tuple<int, int, char>,
  vector<tuple<int, int, char> >,
  greater<tuple<int, int, char> > >
          priorityQueue;
  
  // Declaring a tuple
  tuple<int, int, char> tuple1;
  
  // Initializing the tuple
  tuple1 = make_tuple(1, 1, 'G');
  
  // Pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple1);
  
  // Declaring another tuple
  tuple<int, int, char> tuple2;
  
  // Initializing the tuple
  tuple2 = make_tuple(2, 2, 'e');
  
  // pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple2);
  
  // Declaring another tuple
  tuple<int, int, char> tuple3;
  
  // Initializing the tuple
  tuple3 = make_tuple(3, 3, 'e');
  
  // Pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple3);
  
  // Declaring another tuple
  tuple<int, int, char> tuple4;
  
  // Initializing the tuple
  tuple4 = make_tuple(4, 4, 'k');
  
  // Pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple4);
  
  // Calling print function
  print(priorityQueue);
  
  return 0;
}


Output

[ 1 1 G]
[ 2 2 e]
[ 3 3 e]
[ 4 4 k]

Example 2: Below is the C++ program to implement the working of the min-heap priority queue of tuples.

C++




// C++ program to implement 
// the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to print priority queue
// contents. Deliberately passing it
// call by value since we don't want
// to remove elements from the priority 
// queue
void print(priority_queue<tuple<int, int, string>,
           vector<tuple<int, int, string> >,
           greater<tuple<int, int, string> > >
           priorityQueue)
{
  while (!priorityQueue.empty()) 
  {
    // Each element of the priority
    // queue is a tuple itself
    tuple<int, int, string> Tuple =
          priorityQueue.top();
  
    cout << "[ ";
  
    cout << get<0>(Tuple) << ' ' <<
            get<1>(Tuple) << ' ' << 
            get<2>(Tuple);
    cout << ']';
    cout << '\n';
  
    // Pop out the topmost tuple
    priorityQueue.pop();
  }
}
  
// Driver code
int main()
{
  // Declaring a priority queue of
  // tuple
  priority_queue<tuple<int, int, string>,
  vector<tuple<int, int, string> >,
  greater<tuple<int, int, string> > >
          priorityQueue;
  
  // Declaring a tuple
  tuple<int, int, string> tuple1;
  
  // Initializing the tuple
  tuple1 = make_tuple(0, 0, "Geeks");
  
  // Pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple1);
  
  // Declaring a tuple
  tuple<int, int, string> tuple2;
  
  // Initializing the tuple
  tuple2 = make_tuple(0, 0, 
                      "Programming");
  
  // Pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple2);
  
  // Declaring a tuple
  tuple<int, int, string> tuple3;
  
  // Initializing the tuple
  tuple3 = make_tuple(1, 2, 
                      "Geeks");
  
  // Pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple3);
  
  // Declaring a tuple
  tuple<int, int, string> tuple4;
  
  // Initializing the tuple
  tuple4 = make_tuple(1, 3, 
                      "Programming");
  
  // Pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple4);
  
  // Calling print function
  print(priorityQueue);
  
  return 0;
}


Output

[ 0 0 Geeks]
[ 0 0 Programming]
[ 1 2 Geeks]
[ 1 3 Programming]

Customized priority queue of tuples:

Proceeding further How to implement Min Heap using STL?  is a must pre-requisite to go where here we will be learning how we can customize the priority queue of tuples by passing a comparator to the priority queue as an argument.

Example 1: Below is the C++ program to implement the customized priority queue of tuples.

C++




// C++ program to implement 
// the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Comparator
// we can use class also but then 
// we will need to make the function 
// public as class is by default 
// private
struct myComparator 
{
  int operator()(const tuple<int, int
                 string>& tuple1,
                 const tuple<int, int
                 string>& tuple2)
  {
    // Second element of tuples is equal
    if (get<1>(tuple1) == get<1>(tuple2)) 
    {
      if (get<0>(tuple1) == get<0>(tuple2)) 
      {
        if (get<2>(tuple1) >= get<2>(tuple2))
          return true;
        return false;
      }
  
      return get<0>(tuple1) >= get<0>(tuple2);
    }
  
    return get<1>(tuple1) >= get<1>(tuple2);
  }
};
  
// Function to print priority queue
// contents. Deliberately passing it
// call by value since we don't want
// to remove elements from the priority 
// queue
void print(priority_queue<tuple<int, int
           string>,
           vector<tuple<int, int
           string> >,
           myComparator>
           priorityQueue)
{
  while (!priorityQueue.empty()) 
  {
    // Each element of the priority
    // queue is a tuple itself
    tuple<int, int, string> Tuple = 
          priorityQueue.top();
  
    cout << "[ ";
  
    cout << get<0>(Tuple) << ' ' <<
            get<1>(Tuple) << ' ' << 
            get<2>(Tuple);
    cout << ']';
    cout << '\n';
  
    // Pop out the topmost tuple
    priorityQueue.pop();
  }
}
  
// Driver code
int main()
{
  // Declaring a priority queue 
  // of  tuple
  priority_queue<tuple<int, int, string>,
  vector<tuple<int, int, string> >,
  myComparator> priorityQueue;
  
  // Declaring a tuple
  tuple<int, int, string> tuple1;
  
  // Initializing the tuple
  tuple1 = make_tuple(0, 0, 
                      "Geeks");
  
  // Pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple1);
  
  // Declaring a tuple
  tuple<int, int, string> tuple2;
  
  // Initializing the tuple
  tuple2 = make_tuple(0, 1, 
                      "Programming");
  
  // Pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple2);
  
  // Declaring a tuple
  tuple<int, int, string> tuple3;
  
  // Initializing the tuple
  tuple3 = make_tuple(1, 2, 
                      "Geeks");
  
  // Pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple3);
  
  // Declaring a tuple
  tuple<int, int, string> tuple4;
  
  // Initializing the tuple
  tuple4 = make_tuple(1, 3, 
                      "Programming");
  
  // Pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple4);
  
  // Calling print function
  print(priorityQueue);
  
  return 0;
}


Output

[ 0 0 Geeks]
[ 0 1 Programming]
[ 1 2 Geeks]
[ 1 3 Programming]

In the above program, for a pair of tuples, firstly the second element of tuples are compared and if this value is equal the first element is compared and if the first element of both tuples is equal then the third element is compared. Otherwise, the tuple having a smaller element will become the topmost element.  

Example 2: Below is the C++ program to demonstrate the working of customized priority queue of tuples.

C++




// C++ program to implement 
// the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Comparator
// we can use class also but then 
// we will need to make the function
// public as class is by default private
struct myComparator 
{
  int operator()(const tuple<int, int
                 string>& tuple1,
                 const tuple<int, int
                 string>& tuple2)
  {
    // Second element of tuples is equal
    if (get<1>(tuple1) == get<1>(tuple2)) 
    {
      // first element of tuples is equal
      if (get<0>(tuple1) == get<0>(tuple2)) 
      {
        if (get<2>(tuple1) <= get<2>(tuple2))
          return true;
        return false;
      }
  
      return get<0>(tuple1) <= get<0>(tuple2);
    }
  
    return get<1>(tuple1) <= get<1>(tuple2);
  }
};
  
// Function to print priority queue
// contents. Deliberately passing it
// call by value since we don't want
// to remove elements from the priority 
// queue
void print(priority_queue<tuple<int, int
           string>,
           vector<tuple<int, int, string> >,
           myComparator> priorityQueue)
{
  while (!priorityQueue.empty()) 
  {
    // Each element of the priority
    // queue is a tuple itself
    tuple<int, int, string> Tuple = 
          priorityQueue.top();
  
    cout << "[ ";
  
    cout << get<0>(Tuple) << ' ' << 
            get<1>(Tuple) << ' ' << 
            get<2>(Tuple);
    cout << ']';
    cout << '\n';
  
    // Pop out the topmost tuple
    priorityQueue.pop();
  }
}
  
// Driver code
int main()
{
  // Declaring a priority queue of
  // tuples by passing a custom comparator
  priority_queue<tuple<int, int, string>,
  vector<tuple<int, int, string> >,
  myComparator>
    priorityQueue;
  
  // Declaring a tuple
  tuple<int, int, string> tuple1;
  
  // Initializing the tuple
  tuple1 = make_tuple(0, 0, 
                      "Geeks");
  
  // Pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple1);
  
  // Declaring a tuple
  tuple<int, int, string> tuple2;
  
  // Initializing the tuple
  tuple2 = make_tuple(0, 1, 
                      "Programming");
  
  // Pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple2);
  
  // Declaring a tuple
  tuple<int, int, string> tuple3;
  
  // Initializing the tuple
  tuple3 = make_tuple(1, 2, 
                      "Geeks");
  
  // Pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple3);
  
  // Declaring a tuple
  tuple<int, int, string> tuple4;
  
  // Initializing the tuple
  tuple4 = make_tuple(1, 3, 
                      "Programming");
  
  // Pushing the tuple in the 
  // priority queue
  priorityQueue.push(tuple4);
  
  // Calling print function
  print(priorityQueue);
  
  return 0;
}


Output

[ 1 3 Programming]
[ 1 2 Geeks]
[ 0 1 Programming]
[ 0 0 Geeks]

In the above program, for a pair of tuples, firstly the second element of tuples are compared and if this value is equal then the first element is compared and if the first element of both tuples is equal then the third element is compared. Otherwise, the tuple having a larger element will become the topmost element.  



Last Updated : 23 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads