priority_queue::push() and priority_queue::pop() in C++ STL Last Updated : 24 Oct, 2024 Comments Improve Suggest changes Like Article Like Report 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::push() and priority_queue::pop() methods in C++.Example: C++ // C++ Program to demonstrate use of priority_queue // push() and pop() methods #include <bits/stdc++.h> using namespace std; int main() { priority_queue<int> pq; pq.push(10); pq.push(30); pq.push(20); while(!pq.empty()) { cout << pq.top() << " "; pq.pop(); } return 0; } Output30 20 10 priority_queue::push() MethodThe std::priority_queue::push() method is used to insert (or push) an element in the std::priority queue. As priority queue implements heap data structure, this function inserts the element at the back for queue and then use the heapify up algorithm to maintain the heap property.Syntaxpq.push(val);Parametersval: Value to be inserted/pushed.Return ValueThis function does not return any value.priority_queue::pop() MethodThe priority_queue::pop() function is used to delete (or pop) the top element i.e. the element with highest priority from the priority queue. This function ensures that the heap property is maintained after the deletion of the top element.Syntaxpq.pop();ParametersThis function does not take any parameter.Return ValueThis function does not return any value.More Examples of priority_queue push() and pop()The push() and pop() methods are the primary methods for insertion and deletion of elements in priority_queue container. The below example demonstrates the use of these functions:Example 1: push() and pop() for Min Heap Priority QueueBy default, priority queue uses max heap, but we can change this using custom comparator function. Let's see the working of push and pop in min heap priority queue. C++ // C++ program to use push() and pop() method // with min heap priority queue #include <bits/stdc++.h> using namespace std; int main() { priority_queue<int, vector<int>, greater<int>> pq; // Pushing elements pq.push(10); pq.push(30); pq.push(20); // Printing current top element cout << pq.top() << endl; // Popping the top element pq.pop(); // Printing current top element cout << pq.top(); return 0; } Output10 20Example 2: Trying pop() Method with Empty Priority Queue C++ // C++ Program to demonstrate use of priority_queue // push() and pop() methods #include <bits/stdc++.h> using namespace std; int main() { priority_queue<int> pq; pq.pop(); return 0; } Output(no output)Explanation: The behaviour of pop() method is undefined for empty priority queue so; it is always recommended to check whether the queue is empty or not before using pop() method. Comment More infoAdvertise with us Next Article priority_queue::top() in C++ STL A AyushSaxena Follow Improve Article Tags : Misc C++ STL CPP-Library cpp-containers-library cpp-queue cpp-priority-queue +3 More Practice Tags : CPPMiscSTL 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 Like