How to Delete an Element from a Priority Queue in C++ ? Last Updated : 07 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a priority queue is a queue in which elements are arranged based on their priority values as each element has a priority value associated with it. In this article, we will learn how to delete an element from a priority queue in C++. For Example, Input: myPriorityQueue = {8, 6, 7, 5, 4, 2, 3} Output:myPriorityQueue = {7, 6, 3, 5, 4, 2}Remove Element from Priority Queue in C++ To remove an element from a priority_queue we can use a std::priority_queue::pop() function that directly removes the element with the highest priority (topmost element) from the queue and the element with the next highest priority becomes the top element after deletion. C++ Program to Delete Element from Priority QueueThe below example demonstrates the deletion of an element using pop() in a priority queue. C++ // C++ program to delete an element from priority queue #include <iostream> #include <queue> using namespace std; int main() { // creating a priority queue priority_queue<int> pq; // push element in a priority queue pq.push(10); pq.push(30); pq.push(20); pq.push(50); // printing the top most element before deletion cout << "Top element before removal: " << pq.top() << endl; // Remove the top element pq.pop(); // printing the top most element after deletion cout << "Top element after removal: " << pq.top() << endl; return 0; } OutputTop element before removal: 50 Top element after removal: 30 Time Complexity: O(log N), where N is the number of elements in the priority queue.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Delete an Element from a Priority Queue in C++ ? S shravanngoswamii Follow Improve Article Tags : C++ Programs C++ STL priority-queue cpp-priority-queue CPP Examples +2 More Practice Tags : CPPpriority-queueSTL Similar Reads How to Dequeue an Element from a Queue in C++? In C++, queues are a type of container adaptors that operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. In this article, we will learn how to dequeue an element from a queue in C++ STL. Example: Input: Create a queue an 2 min read How to Delete an Element from a Set in C++? A set in C++ is a container that stores unique elements in a sorted order. In this article, we will learn how to delete a specific element from a set. Example: Input: mySet = {5, 2, 8, 1, 4} Element to delete: 2 Output: mySet = {5, 1, 8, 4}Delete an Element from a Set in C++To delete a specific elem 2 min read How to Delete an Element from a Multiset in C++? In C++, a multiset is a container that stores elements in a specific order. Multiple elements can have the same values. In this article, we will learn how to delete a specific element from a multiset. Example: Input: myMultiset = {5, 2, 8, 5, 8, 8}; Element to delete: 8 Output: myMultiset = {5, 2, 5 2 min read How to Remove an Element from a Deque in C++? In C++ STL, a container called deque (known as a double-ended queue) allows us to insert and delete elements at both its beginning and its end. In this article, we will learn how to remove a specific element from a deque in C++ STL. Example: Input: myDeque= {4, 2, 3, 5, 2} Target = 4 Output: Deque A 2 min read How to Delete an Element from an Array in C++? In C++, arrays are data structures that allow users to store data of the same type in contiguous memory locations. In this article, we will learn how to delete an element from an array in C++. Example: Input:myarr = {20, 5, 1, 10, 15};Target: 1Output:Array after deletion: 20 5 10 15Remove an Element 3 min read Like