Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Priority Queue of Maps in C++ with Examples

  • Last Updated : 23 Nov, 2021

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 non-increasing order (hence we can see that each element of the queue has a priority {fixed order}). Generally, priority queues are of two types: max-heap and min-heap but we can always pass a comparator to a priority queue. 

Functions used with 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.

Map

Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have the same key values. Internally it is implemented as self-balancing BST.

Functions used with Map:

  • begin(): Returns an iterator to the first element in the map
  • end(): Returns an iterator to the theoretical element that follows the last element in the map
  • size(): Returns the number of elements in the map
  • empty(): Returns whether the map is empty

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

Max-heap priority queue of maps:

  • Max-heap priority queue in which maps are arranged in non-descending order:

priority_queue<map<data_type1, data_type2>> priorityQueue;

Here, 
data_type1: is the type of data for key.
data_type2: is the type of data for value.

  • Max-heap priority queue in which maps are arranged in non-ascending order:

priority_queue<map<data_type1, data_type2 , greater<data_type1>>> priorityQueue;

Here, 
data_type1: is the type of data for key.
data_type2: is the type of data for value.

Min-heap priority queue of maps:

  • Min-heap priority queue in which maps are arranged in non-descending order:

priority_queue<map<data_type1, data_type2>, vector<map<data_type1, data_type2>>, 
                           greater<map<data_type1, data_type2>>> priorityQueue;

Here, 
data_type1: is the data type for key.
data_type2: is the data type for value.

  • Min-heap priority queue in which maps are arranged in non-ascending order:

priority_queue<map<data_type1, data_type2>, vector<map<data_type1, data_type2, greater<data_type1>>>,
                           greater<map<data_type1, data_type2, greater<data_type1>>>> priorityQueue;

Here, 
data_type1: is the data type for key.
data_type2: is the data type for value.

Customized priority queue of maps (using comparator):

priority_queue<map<data_type1, data_type2>, vector<map<data_type1, data_type2>>, 
                        myComparator> priorityQueue;

Here, 
data_type1: is the data type for key.
data_type2: is the data type for value.
myComparator: is the comparator class or struct.

Below is the C++ code snippet for the comparator:

C++




// Comparator structure
struct myComparator
{
  int operator()(const map<data_type1, 
                           data_type2>& map1,
                 const map<data_type1, 
                           data_type2>& map2)
  {
    // body
  }
};
  
// Comparator class
struct myComparator 
{
  public:
  int operator()(const map<data_type1, 
                           data_type2>& map1,
                 const map<data_type1, 
                           data_type2>& map2)
  {
    // body
  }
};

 

Max-heap priority queue of maps

By default priority queue is max-heap. So, In the case of a pair of maps in the max-heap priority queue, the key and value of the two map elements are compared simultaneously, one by one. If the key of the first element of the two maps is equal then the value of the first element is compared, and if the value of the first element of both the maps is also equal then the key of the second element is compared If the key of the second element of the two maps is also equal then the value of the second element is compared and so on. The map having the larger key or value during the comparison becomes the topmost element of the priority queue.

Below are the C++ programs to demonstrate the working of the max-heap priority queue of maps such that maps elements are sorted in non-descending order by the key:

Example 1:

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<map<int, int> > 
           priorityQueue)
{
  while (!priorityQueue.empty()) 
  {
    // Each element of the priority
    // queue is a map itself
    map<int, int> map1 = priorityQueue.top();
  
    cout << "[    ";
  
    // Print the map elements
    for (auto pair1 : map1) 
    {
      // Each element in a map is a key,
      // value pair
      cout << "key:" << pair1.first << " , " << 
              "value:" << pair1.second << "    ";
    }
      
    cout << ']';
    cout << '\n';
  
    // Pop out the topmost map
    priorityQueue.pop();
  }
}
  
// Driver code
int main()
{
  // Priority queue of map
  // Each element of the priority 
  // queue is a map
  priority_queue<map<int, int> > 
  priorityQueue;
  
  // Declaring a map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, int> map1;
  
  map1[1] = 11;
  map1[2] = 22;
  map1[3] = 33;
  
  priorityQueue.push(map1);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, int> map2;
  
  map2[1] = 11;
  map2[2] = 22;
  map2[3] = 44;
  
  priorityQueue.push(map2);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, int> map3;
  
  map3[1] = 11;
  map3[2] = 22;
  map3[4] = 33;
  
  priorityQueue.push(map3);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, int> map4;
  
  map4[11] = 11;
  map4[2] = 22;
  map4[4] = 33;
  
  priorityQueue.push(map4);
  
  // Calling print function
  print(priorityQueue);
  
  return 0;
}

Output:

[    key:2 , value:22    key:4 , value:33    key:11 , value:11    ]
[    key:1 , value:11    key:2 , value:22    key:4 , value:33    ]
[    key:1 , value:11    key:2 , value:22    key:3 , value:44    ]
[    key:1 , value:11    key:2 , value:22    key:3 , value:33    ]

Example 2: 

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<map<int, string> > 
           priorityQueue)
{
  while (!priorityQueue.empty()) 
  {
    // Each element of the priority
    // queue is a map itself
    map<int, string> map1 = 
        priorityQueue.top();
  
    cout << "[    ";
  
    // Print the map elements
    for (auto pair1 : map1) 
    {
      // Each element in a map is a key,
      // value pair
      cout << "key:" << pair1.first << " , " << 
              "value:" << pair1.second << "    ";
    }
      
    cout << ']';
    cout << '\n';
  
    // Pop out the topmost map
    priorityQueue.pop();
  }
}
  
// Driver code
int main()
{
  // Priority queue of map
  // Each element of the priority 
  // queue is a map
  priority_queue<map<int, string> > 
  priorityQueue;
  
  // Declaring a map whose key is 
  // integer type and, value is 
  // also integer type
  map<int, string> map1;
  
  map1[1] = "geeks";
  map1[2] = "for";
  map1[3] = "geeks";
  
  priorityQueue.push(map1);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, string> map2;
  
  map2[1] = "geeks";
  map2[2] = "for";
  map2[3] = "learning";
  
  priorityQueue.push(map2);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, string> map3;
  
  map3[1] = "geeks";
  map3[2] = "for";
  map3[4] = "geeks";
  priorityQueue.push(map3);
  
  // Calling print function
  print(priorityQueue);
  
  return 0;
}

Output:

[    key:1 , value:geeks    key:2 , value:for    key:4 , value:geeks    ]
[    key:1 , value:geeks    key:2 , value:for    key:3 , value:learning    ]
[    key:1 , value:geeks    key:2 , value:for    key:3 , value:geeks    ]

Max Heap Priority Queue of Maps

Below are the C++ programs to demonstrate the working of the max-heap priority queue of maps such that maps elements are sorted in non-ascending order by the key:

Example 1:

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<map<int, int
           greater<int> > >
           priorityQueue)
{
  while (!priorityQueue.empty()) 
  {
    // Each element of the priority
    // queue is a map itself
    map<int, int, greater<int> > map1 = 
                  priorityQueue.top();
  
    cout << "[    ";
  
    // Print the map elements
    for (auto pair1 : map1) 
    {
      // Each element in a map is a key,
      // value pair
      cout << "key:" << pair1.first << " , " << 
              "value:" << pair1.second << "    ";
    }
      
    cout << ']';
    cout << '\n';
  
    // Pop out the topmost map
    priorityQueue.pop();
  }
}
  
// Driver code
int main()
{
  // Priority queue of map
  // Each element of the priority 
  // queue is a map
  priority_queue<map<int, int
                 greater<int> > >
                 priorityQueue;
  
  // Declaring a map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, int, greater<int> > map1;
  
  map1[1] = 11;
  map1[2] = 22;
  map1[3] = 33;
  
  priorityQueue.push(map1);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, int, greater<int> > map2;
  
  map2[1] = 11;
  map2[2] = 22;
  map2[3] = 44;
  
  priorityQueue.push(map2);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, int, greater<int> > map3;
  
  map3[1] = 11;
  map3[2] = 22;
  map3[4] = 33;
  
  priorityQueue.push(map3);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, int, greater<int> > map4;
  
  map4[1] = 11;
  map4[2] = 22;
  map4[4] = 33;
  
  priorityQueue.push(map4);
  
  // Calling print function
  print(priorityQueue);
  
  return 0;
}

Output:

[    key:4 , value:33    key:2 , value:22    key:1 , value:11    ]
[    key:4 , value:33    key:2 , value:22    key:1 , value:11    ]
[    key:3 , value:44    key:2 , value:22    key:1 , value:11    ]
[    key:3 , value:33    key:2 , value:22    key:1 , value:11    ]

Example 2:

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<map<int, string, 
           greater<int> > >
           priorityQueue)
{
  while (!priorityQueue.empty()) 
  {
    // Each element of the priority
    // queue is a map itself
    map<int, string, greater<int> > map1 = 
                     priorityQueue.top();
  
    cout << "[    ";
  
    // Print the map elements
    for (auto pair1 : map1) 
    {
      // Each element in a map is a key,
      // value pair
      cout << "key:" << pair1.first << " , " << 
              "value:" << pair1.second << "    ";
    }
      
    cout << ']';
    cout << '\n';
  
    // Pop out the topmost map
    priorityQueue.pop();
  }
}
  
// Driver code
int main()
{
  // Priority queue of map
  // Each element of the priority 
  // queue is a map
  priority_queue<map<int, string, 
                 greater<int> > >
                 priorityQueue;
  
  // Declaring a map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, string, greater<int> > map1;
  
  map1[1] = "geeks";
  map1[2] = "for";
  map1[3] = "geeks";
  
  priorityQueue.push(map1);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, string, greater<int> > map2;
  
  map2[1] = "geeks";
  map2[2] = "for";
  map2[3] = "learning";
  
  priorityQueue.push(map2);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, string, greater<int> > map3;
  
  map3[1] = "geeks";
  map3[2] = "for";
  map3[4] = "geeks";
  priorityQueue.push(map3);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, string, greater<int> > map4;
  
  map4[1] = "Code";
  map4[2] = "For";
  map4[4] = "Fun";
  priorityQueue.push(map4);
  
  // Calling print function
  print(priorityQueue);
  
  return 0;
}

Output:

[    key:4 , value:geeks    key:2 , value:for    key:1 , value:geeks    ]
[    key:4 , value:Fun    key:2 , value:For    key:1 , value:Code    ]
[    key:3 , value:learning    key:2 , value:for    key:1 , value:geeks    ]
[    key:3 , value:geeks    key:2 , value:for    key:1 , value:geeks    ]

Min-heap priority queue of maps

In the case of a pair of maps in the min-heap priority queue, the key and value of the two map elements are compared simultaneously, one by one. If the key of the first element of the two maps is equal then the value of the first element is compared, and if the value of the first element of both the maps is also equal then the key of the second element is compared If the key of the second element of the two maps is also equal then the value of the second element is compared and so on. The map having the smaller key or value during the comparison becomes the topmost element of the priority queue.

Below are the C++ programs to demonstrate the working of the min-heap priority queue of maps such that map elements are arranged in non-descending order by the key:

Example 1:

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<map<int, int>, 
           vector<map<int, int> >,
           greater<map<int, int> > > 
           priorityQueue)
{
  while (!priorityQueue.empty()) 
  {
    // Each element of the priority
    // queue is a map itself
    map<int, int> map1 = priorityQueue.top();
  
    cout << "[    ";
  
    // Print the map elements
    for (auto pair1 : map1) 
    {
      // Each element in a map is a key,
      // value pair
      cout << "key:" << pair1.first << " , " << 
              "value:" << pair1.second << "    ";
    }
      
    cout << ']';
    cout << '\n';
  
    // Pop out the topmost map
    priorityQueue.pop();
  }
}
  
// Driver code
int main()
{
  // Priority queue of map
  // Each element of the priority 
  // queue is a map
  priority_queue<map<int, int>, 
                 vector<map<int, int> >,
                 greater<map<int, int> > > 
                 priorityQueue;
  
  // Declaring a map whose key is 
  // integer type and, value is 
  // also integer type
  map<int, int> map1;
  
  map1[1] = 11;
  map1[2] = 22;
  map1[3] = 33;
  
  priorityQueue.push(map1);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, int> map2;
  
  map2[1] = 11;
  map2[2] = 22;
  map2[3] = 44;
  
  priorityQueue.push(map2);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, int> map3;
  
  map3[1] = 11;
  map3[2] = 22;
  map3[4] = 33;
  
  priorityQueue.push(map3);
  
  // Calling print function
  print(priorityQueue);
  
  return 0;
}

Output:

[    key:1 , value:11    key:2 , value:22    key:3 , value:33    ]
[    key:1 , value:11    key:2 , value:22    key:3 , value:44    ]
[    key:1 , value:11    key:2 , value:22    key:4 , value:33    ]

Example 2:

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<map<int, string>,
           vector<map<int, string> >,
           greater<map<int, string> > >
           priorityQueue)
{
  while (!priorityQueue.empty()) 
  {
    // Each element of the priority
    // queue is a map itself
    map<int, string> map1 =
             priorityQueue.top();
  
    cout << "[    ";
  
    // Print the map elements
    for (auto pair1 : map1) 
    {
      // Each element in a map is a key,
      // value pair
      cout << "key:" << pair1.first << " , " << 
              "value:" << pair1.second << "    ";
    }
    cout << ']';
    cout << '\n';
  
    // Pop out the topmost map
    priorityQueue.pop();
  }
}
  
// Driver code
int main()
{
  // Priority queue of map
  // Each element of the priority 
  // queue is a map
  priority_queue<map<int, string>, 
                 vector<map<int, string> >,
                 greater<map<int, string> > > 
                 priorityQueue;
  
  // Declaring a map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, string> map1;
  
  map1[1] = "geeks";
  map1[2] = "for";
  map1[3] = "geeks";
  
  priorityQueue.push(map1);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, string> map2;
  
  map2[1] = "geeks";
  map2[2] = "for";
  map2[3] = "learning";
  
  priorityQueue.push(map2);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, string> map3;
  
  map3[1] = "geeks";
  map3[2] = "for";
  map3[4] = "geeks";
  priorityQueue.push(map3);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, string> map4;
  
  map4[1] = "Code";
  map4[2] = "For";
  map4[4] = "Fun";
  priorityQueue.push(map4);
  
  // Calling print function
  print(priorityQueue);
  
  return 0;
}

Output:

[    key:1 , value:Code    key:2 , value:For    key:4 , value:Fun    ]
[    key:1 , value:geeks    key:2 , value:for    key:3 , value:geeks    ]
[    key:1 , value:geeks    key:2 , value:for    key:3 , value:learning    ]
[    key:1 , value:geeks    key:2 , value:for    key:4 , value:geeks    ]

Min Heap Priority Queue of Maps

Below are the C++ programs to demonstrate the working of the min-heap priority queue of maps such that map elements are arranged in non-ascending order by the key:

Example 1:

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<map<int, int>,
           vector<map<int, int
           greater<int> > >,
           greater<map<int, int
           greater<int> > > >
           priorityQueue)
{
  while (!priorityQueue.empty()) 
  {
    // Each element of the priority
    // queue is a map itself
    map<int, int, greater<int> > map1 = 
                  priorityQueue.top();
  
    cout << "[    ";
  
    // Print the map elements
    for (auto pair1 : map1) 
    {
      // Each element in a map is a key,
      // value pair
      cout << "key:" << pair1.first << " , " << 
              "value:" << pair1.second << "    ";
    }
      
    cout << ']';
    cout << '\n';
  
    // Pop out the topmost map
    priorityQueue.pop();
  }
}
  
// Driver code
int main()
{
  // Priority queue of map
  // Each element of the priority 
  // queue is a map
  priority_queue<map<int, int>,
                 vector<map<int, int
                 greater<int> > >,
                 greater<map<int, int
                 greater<int> > > >
                 priorityQueue;
  
  // Declaring a map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, int, greater<int> > map1;
  
  map1[1] = 11;
  map1[2] = 22;
  map1[3] = 33;
  
  priorityQueue.push(map1);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, int, greater<int> > map2;
  
  map2[1] = 11;
  map2[2] = 22;
  map2[3] = 44;
  
  priorityQueue.push(map2);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, int, greater<int> > map3;
  
  map3[1] = 11;
  map3[2] = 22;
  map3[4] = 33;
  
  priorityQueue.push(map3);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, int, greater<int> > map4;
  
  map4[1] = 11;
  map4[2] = 22;
  map4[4] = 33;
  
  priorityQueue.push(map4);
    
  // Calling print function
  print(priorityQueue);
  
  return 0;
}

Output:

[    key:3 , value:33    key:2 , value:22    key:1 , value:11    ]
[    key:3 , value:44    key:2 , value:22    key:1 , value:11    ]
[    key:4 , value:33    key:2 , value:22    key:1 , value:11    ]
[    key:4 , value:33    key:2 , value:22    key:1 , value:11    ]

Example 2:

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<
           map<int, string, 
           greater<int> >,
           vector<map<int, string, 
           greater<int> > >,
           greater<map<int, string, 
           greater<int> > > >
           priorityQueue)
{
  while (!priorityQueue.empty()) 
  {
    // Each element of the priority
    // queue is a map itself
    map<int, string, greater<int> > map1 = 
                     priorityQueue.top();
  
    cout << "[    ";
  
    // Print the map elements
    for (auto pair1 : map1)
    {
      // Each element in a map is a key,
      // value pair
      cout << "key:" << pair1.first << " , " << 
              "value:" << pair1.second << "    ";
    }
      
    cout << ']';
    cout << '\n';
  
    // Pop out the topmost map
    priorityQueue.pop();
  }
}
  
// Driver code
int main()
{
  // Priority queue of map
  // Each element of the priority 
  // queue is a map
  priority_queue<map<int, string, 
                 greater<int> >,
                 vector<map<int, string, 
                 greater<int> > >,
                 greater<map<int, string, 
                 greater<int> > > >
                 priorityQueue;
  
  // Declaring a map whose key is 
  // integer type and, value is 
  // also integer type
  map<int, string, greater<int> > map1;
  
  map1[1] = "geeks";
  map1[2] = "for";
  map1[3] = "geeks";
  
  priorityQueue.push(map1);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, string, greater<int> > map2;
  
  map2[1] = "geeks";
  map2[2] = "for";
  map2[3] = "learning";
  
  priorityQueue.push(map2);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, string, greater<int> > map3;
  
  map3[1] = "geeks";
  map3[2] = "for";
  map3[4] = "geeks";
  priorityQueue.push(map3);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, string, greater<int> > map4;
  
  map4[1] = "Code";
  map4[2] = "For";
  map4[4] = "Fun";
  priorityQueue.push(map4);
  
  // Calling print function
  print(priorityQueue);
  
  return 0;
}

Output:

[    key:3 , value:geeks    key:2 , value:for    key:1 , value:geeks    ]
[    key:3 , value:learning    key:2 , value:for    key:1 , value:geeks    ]
[    key:4 , value:Fun    key:2 , value:For    key:1 , value:Code    ]
[    key:4 , value:geeks    key:2 , value:for    key:1 , value:geeks    ]

Customized priority queue of maps

Below are the C++ programs to demonstrate the working of the customized priority queue of maps:

Example 1:

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
  
struct myComparator 
{
  int operator()(const map<int, int>& map1,
                 const map<int, int>& map2)
  {
    // Max-heap priority queue
    for (auto it1 = map1.begin(), 
              it2 = map2.begin();
              it1 != map1.end(), 
              it2 != map2.end();
              it1++, it2++) 
    {
      auto key1 = it1->first;
      auto value1 = it1->second;
  
      auto key2 = it2->first;
      auto value2 = it2->second;
  
      if (key1 == key2) 
      {
        if (value1 == value2)
          continue;
        return value1 < value2;
      }
  
      return key1 < key2;
    }
  
    return map1.size() <= map2.size();
  }
};
  
// Function to print priority 
// queue contents
void print(priority_queue<map<int, int>, 
           vector<map<int, int> >,
           myComparator> priorityQueue)
{
  while (!priorityQueue.empty()) 
  {
    // Each element of the priority
    // queue is a map itself
    map<int, int> map1 = 
             priorityQueue.top();
  
    cout << "[    ";
  
    // Print the map elements
    for (auto pair1 : map1) 
    {
      // Each element in a map is a key,
      // value pair
      cout << "key:" << pair1.first << " , " << 
              "value:" << pair1.second << "    ";
    }
      
    cout << ']';
    cout << '\n';
  
    // Pop out the topmost map
    priorityQueue.pop();
  }
}
  
// Driver code
int main()
{
  // Priority queue of map
  // Each element of the priority 
  // queue is a map
  priority_queue<map<int, int>, 
                 vector<map<int, int> >,
                 myComparator>
                 priorityQueue;
  
  // Declaring a map whose key is 
  // integer type and, value is 
  // also integer type
  map<int, int> map1;
  
  map1[1] = 11;
  map1[2] = 22;
  map1[3] = 33;
  
  priorityQueue.push(map1);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, int> map2;
  
  map2[1] = 11;
  map2[2] = 22;
  map2[3] = 44;
  
  priorityQueue.push(map2);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, int> map3;
  
  map3[1] = 11;
  map3[2] = 22;
  map3[4] = 33;
  
  priorityQueue.push(map3);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, int> map4;
  
  map4[1] = 11;
  map4[2] = 22;
  map4[4] = 33;
  
  priorityQueue.push(map4);
  
  // Calling print function
  print(priorityQueue);
  
  return 0;
}

Output:

[    key:1 , value:11    key:2 , value:22    key:4 , value:33    ]
[    key:1 , value:11    key:2 , value:22    key:4 , value:33    ]
[    key:1 , value:11    key:2 , value:22    key:3 , value:44    ]
[    key:1 , value:11    key:2 , value:22    key:3 , value:33    ]

Example 2:

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
  
struct myComparator 
{
  int operator()(const map<int, int>& map1,
                 const map<int, int>& map2)
  {
    // Min-heap priority queue
    for (auto it1 = map1.begin(), 
              it2 = map2.begin();
              it1 != map1.end(), 
              it2 != map2.end();
              it1++, it2++) 
    {
      auto key1 = it1->first;
      auto value1 = it1->second;
  
      auto key2 = it2->first;
      auto value2 = it2->second;
  
      if (key1 == key2) 
      {
        if (value1 == value2)
          continue;
        return value1 > value2;
      }
      return key1 > key2;
    }
    return map1.size() >= map2.size();
  }
};
  
// Function to print priority 
// queue contents
void print(priority_queue<map<int, int>, 
           vector<map<int, int> >,
           myComparator> priorityQueue)
{
  while (!priorityQueue.empty()) 
  {
    // Each element of the priority
    // queue is a map itself
    map<int, int> map1 = 
                  priorityQueue.top();
  
    cout << "[    ";
  
    // Print the map elements
    for (auto pair1 : map1) 
    {
      // Each element in a map is a key,
      // value pair
      cout << "key:" << pair1.first << " , " << 
              "value:" << pair1.second << "    ";
    }
      
    cout << ']';
    cout << '\n';
  
    // Pop out the topmost map
    priorityQueue.pop();
  }
}
  
// Driver code
int main()
{
  // Priority queue of map
  // Each element of the priority 
  // queue is a map
  priority_queue<map<int, int>, 
                 vector<map<int, int> >,
                 myComparator> priorityQueue;
  
  // Declaring a map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, int> map1;
  
  map1[1] = 11;
  map1[2] = 22;
  map1[3] = 33;
  
  priorityQueue.push(map1);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, int> map2;
  
  map2[1] = 11;
  map2[2] = 22;
  map2[3] = 44;
  
  priorityQueue.push(map2);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, int> map3;
  
  map3[1] = 11;
  map3[2] = 22;
  map3[4] = 33;
  
  priorityQueue.push(map3);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, int> map4;
  
  map4[1] = 11;
  map4[2] = 22;
  map4[4] = 33;
  
  priorityQueue.push(map4);
  
  // Calling print function
  print(priorityQueue);
  
  return 0;
}

Output:

[    key:1 , value:11    key:2 , value:22    key:3 , value:33    ]
[    key:1 , value:11    key:2 , value:22    key:3 , value:44    ]
[    key:1 , value:11    key:2 , value:22    key:4 , value:33    ]
[    key:1 , value:11    key:2 , value:22    key:4 , value:33    ]

Example 3:

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
  
struct myComparator 
{
  int operator()(const map<int, string>& map1,
                 const map<int, string>& map2)
  {
    // Max-heap priority queue
    for (auto it1 = map1.begin(), 
              it2 = map2.begin();
              it1 != map1.end(), 
              it2 != map2.end();
              it1++, it2++) 
    {
      auto key1 = it1->first;
      auto value1 = it1->second;
  
      auto key2 = it2->first;
      auto value2 = it2->second;
  
      if (key1 == key2) 
      {
        if (value1 == value2)
          continue;
        return value1 < value2;
      }
      return key1 < key2;
    }
    return map1.size() <= map2.size();
  }
};
  
// Function to print priority 
// queue contents
void print(priority_queue<map<int, string>,
           vector<map<int, string> >, 
           myComparator> priorityQueue)
{
  while (!priorityQueue.empty()) 
  {
    // Each element of the priority
    // queue is a map itself
    map<int, string> map1 = 
                     priorityQueue.top();
  
    cout << "[    ";
  
    // Print the map elements
    for (auto pair1 : map1) 
    {
      // Each element in a map is a key,
      // value pair
      cout << "key:" << pair1.first << " , " << 
              "value:" << pair1.second << "    ";
    }
      
    cout << ']';
    cout << '\n';
  
    // Pop out the topmost map
    priorityQueue.pop();
  }
}
  
// Driver code
int main()
{
  // Priority queue of map
  // Each element of the priority 
  // queue is a map
  priority_queue<map<int, string>,
                 vector<map<int, string> >, 
                 myComparator> priorityQueue;
  
  // Declaring a map whose key is 
  // integer type and, value is 
  // also integer type
  map<int, string> map1;
  
  map1[1] = "apple";
  map1[2] = "boy";
  map1[3] = "cat";
  
  priorityQueue.push(map1);
  
  // Declaring another map whose key 
  // is integer type and, value is 
  // also integer type
  map<int, string> map2;
  
  map2[1] = "apple";
  map2[2] = "bat";
  map2[4] = "eat";
  
  priorityQueue.push(map2);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, string> map3;
  
  map3[1] = "apple";
  map3[2] = "ao";
  map3[10] = "fo";
  priorityQueue.push(map3);
  
  // Declaring another map whose key is 
  // integer type and, value is also
  // integer type
  map<int, string> map4;
  
  map4[1] = "code";
  map4[2] = "for";
  map4[10] = "fun";
  priorityQueue.push(map4);
  
  // Calling print function
  print(priorityQueue);
  
  return 0;
}

Output:

[    key:1 , value:code    key:2 , value:for    key:10 , value:fun    ]
[    key:1 , value:apple    key:2 , value:boy    key:3 , value:cat    ]
[    key:1 , value:apple    key:2 , value:bat    key:4 , value:eat    ]
[    key:1 , value:apple    key:2 , value:ao    key:10 , value:fo    ]

Example 4:

C++




// C++ program to implement 
// the above approach
#include <bits/stdc++.h>
using namespace std;
  
struct myComparator 
{
  int operator()(const map<int, string>& map1,
                 const map<int, string>& map2)
  {
    // Min-heap priority queue
    for (auto it1 = map1.begin(),  
              it2 = map2.begin(); 
              it1 != map1.end(), 
              it2 != map2.end(); 
              it1++, it2++) 
    {
      auto key1 = it1->first;
      auto value1 = it1->second;
  
      auto key2 = it2->first;
      auto value2 = it2->second;
  
      if (key1 == key2) 
      {
        if (value1 == value2)
          continue;
        return value1 > value2;
      }
      return key1 > key2;
    }
    return map1.size() >= map2.size();
  }
};
  
// Function to print priority 
// queue contents
void print(priority_queue<map<int, string>,
                         vector<map<int, string> >, 
                         myComparator> priorityQueue)
{
  while (!priorityQueue.empty()) 
  {
    // Each element of the priority
    // queue is a map itself
    map<int, string> map1 = priorityQueue.top();
  
    cout << "[    ";
  
    // Print the map elements
    for (auto pair1 : map1) 
    {
      // Each element in a map is a key,
      // value pair
      cout << "key:" << pair1.first << " , "
        << "value:" << pair1.second << "    ";
    }
      
    cout << ']';
    cout << '\n';
  
    // Pop out the topmost map
    priorityQueue.pop();
  }
}
  
// Driver code
int main()
{
  // Priority queue of map
  // Each element of the priority 
  // queue is a map
  priority_queue<map<int, string>,
                 vector<map<int, string> >, 
                 myComparator> priorityQueue;
  
  // Declaring a map whose key is 
  // integer type and, value is 
  // also integer type
  map<int, string> map1;
  
  map1[1] = "apple";
  map1[2] = "boy";
  map1[3] = "cat";
  
  priorityQueue.push(map1);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, string> map2;
  
  map2[1] = "apple";
  map2[2] = "bat";
  map2[4] = "eat";
  
  priorityQueue.push(map2);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, string> map3;
  
  map3[1] = "apple";
  map3[2] = "ao";
  map3[10] = "fo";
  priorityQueue.push(map3);
  
  // Declaring another map whose key is 
  // integer type and, value is also 
  // integer type
  map<int, string> map4;
  
  map4[1] = "code";
  map4[2] = "for";
  map4[10] = "fun";
  priorityQueue.push(map4);
    
  // Calling print function
  print(priorityQueue);
  
  return 0;
}

Output:

[    key:1 , value:apple    key:2 , value:ao    key:10 , value:fo    ]
[    key:1 , value:apple    key:2 , value:bat    key:4 , value:eat    ]
[    key:1 , value:apple    key:2 , value:boy    key:3 , value:cat    ]
[    key:1 , value:code    key:2 , value:for    key:10 , value:fun    ]


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!