C++ priority_queue - push() Function
The C++ priority_queue::push function is used to add a new element in the priority_queue. Each insertion of element increases the priority_queue container size by one.
priority_queue is similar to a heap, where elements can be inserted in any order and max heap element is retrieved first. Adding a new element effectively calls push_back of the underlying container, and then reorders the new element to its location in the heap by calling the push_heap algorithm on the range including all the elements of the container.
Syntax
void push (const value_type& val);
void push (const value_type& val); void push (value_type&& val);
Parameters
val |
Specify value which need to be added in the priority_queue. |
Return Value
None.
Time Complexity
Logarithmic i.e, Θ(log(n)).
Example:
In the example below, the priority_queue::push function is used to add new elements in the priority_queue called pqueue.
#include <iostream> #include <queue> using namespace std; int main (){ priority_queue<int> pqueue; //add new elements in the priority_queue pqueue.push(100); pqueue.push(80); pqueue.push(990); pqueue.push(85); pqueue.push(10); cout<<"pqueue contains:"; while (!pqueue.empty()) { cout<<" "<<pqueue.top(); pqueue.pop(); } return 0; }
The output of the above code will be:
pqueue contains: 990 100 85 80 10
❮ C++ <queue> Library