How to Implement Priority Queue Using Multimap in C++? Last Updated : 16 Feb, 2024 Comments Improve Suggest changes Like Article Like Report A priority queue is a type of queue where elements are removed from the queue based on some priority. The element with the highest (or lowest priority depending on the type) will be removed first. Priority queues are fundamental data structures and are used in many applications like CPU scheduling, graph algorithms, and many others. In this article, we will learn how to implement a priority queue using multimap in C++. Priority Queue using Multimap in C++Multimaps are the containers that store the sorted data in the form of key-value pairs. We can create a priority queue using multimaps because they both use binary tree data structures in their implementation. We will have to perform the following basic operations in the given time complexity: Push: O(log n)Pop: O(log n)Peek: O(1)empty: O(1)For that, we will be using the keys as the priority and the data item as the values. The following program implements the min heap priority queue where the smallest key will be given the most priority and so on. C++ Program to Implement Priority Queue using Multimap C++ // C++ program to implement a priority queue using a // multimap #include <iostream> #include <map> using namespace std; template <typename T, typename Priority> class PriorityQueue { private: multimap<Priority, T> elements; public: // Method to push an element with its priority to the // priority queue void push(const T& element, const Priority& priority) { elements.emplace(priority, element); } // Method to pop the element with the highest priority // from the priority queue T pop() { if (elements.empty()) { throw out_of_range("Priority queue is empty"); } auto highestPriority = elements.begin(); T element = highestPriority->second; elements.erase(highestPriority); return element; } // Method to peek at the element with the highest // priority without removing it T peek() const { if (elements.empty()) { throw out_of_range("Priority queue is empty"); } return elements.begin()->second; } // Method to check if the priority queue is empty bool empty() const { return elements.empty(); } }; int main() { // Create a priority queue of strings with integers as // priorities PriorityQueue<string, int> pq; pq.push("Task 1", 3); pq.push("Task 2", 1); pq.push("Task 3", 2); pq.push("Task 4", 4); // Peek at the task with the highest priority cout << "Peeked task: " << pq.peek() << endl; // Pop and print tasks until the priority queue is empty while (!pq.empty()) { cout << pq.pop() << endl; } return 0; } OutputPeeked task: Task 2 Task 2 Task 3 Task 1 Task 4 Comment More infoAdvertise with us Next Article How to Implement Priority Queue Using Multimap in C++? S shivanshmahajan876 Follow Improve Article Tags : C++ Programs C++ STL cpp-multimap cpp-priority-queue CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Insert into Multimap using make_pair in C++? In C++, a multimap is a container that stores key-value pairs in an ordered manner. Unlike a map, a multimap allows multiple values to be associated with a single key. In this article, we will learn how to insert into a multimap using make_pair in C++. Example Input: myMultimap = {{1, âC++â}, {2, âJ 2 min read How to Insert a Pair in Multimap in C++? In C++, we have multimap which is used to store key-value pairs like a map but multimap can have multiple values for the same key. In this article, we will learn how to insert a pair into a multimap in C++. Example Input: myMultimap = { {1, "this"}, {2,"is"}} myPair = {2, "was"}; Output: myMultimap 2 min read How to Find the Union of Two Multimaps in C++? In C++, finding the union of two multimaps consists of combining the elements from both multimap collections while considering the duplicates as a multimap allows multiple values to have the same key. In this article, we will learn to find the union of two multimaps in C++. Example:Input: multi1 = { 2 min read How to Replace a Specific Pair in a Multimap in C++? in C++, multimap is similar to a map that stores the data in the key-value format where duplicate keys are allowed. In this article, we will learn how to replace a specific pair in a multimap in C++. Example Input: myMultimap = {{1, âoneâ}, {2, âtwoâ}, {2, âtwoâ}, {3, âthreeâ}}; Key-Value Pair to Re 3 min read How to Store Vectors as Keys in a Multimap in C++? In C++, the std::multimap is a container that stores elements in a key-value pair, whereas std::vector is a sequence container that stores elements in contiguous memory. In this article, we will learn how to store vectors as keys in a multimap in C++. Example: Input:myVector ={1,2,3};myVector ={4,5, 2 min read Like