In C++, push() adds one new element to a std::priority_queue. You do not choose the position. The queue stores the value and moves it into the right place so that the highest-priority element is always ready on top.
The order you push in does not matter. You can push 10, then 50, then 20, and the queue still gives back 50 first. By default the biggest value has the highest priority, because the queue is a max-heap.
This function is a member function of std::priority_queue<T>, declared in the <queue> header. It has been available since the first C++ standard, so every compiler supports it. You call it with a dot, for example pq.push(...).
Syntax
Call push() with the value you want to add. Two overloads exist: one copies the value, the other moves it.
C++
std::priority_queue<int> pq;
pq.push(10);
pq.push(50); // 50 becomes the top
pq.push(20);
int biggest = pq.top(); // 50
Call
What it does
pq.push(value)
Copies value into the queue, then restores the heap order.
pq.push(std::move(value))
Moves value into the queue. It is faster for big objects such as long strings.
push() returns nothing, so you cannot use the result. Each call takes O(log n) time, where n is the number of elements. If you want to build the object right inside the queue, see emplace().
Library declarations
C++
void push(const value_type& value);
void push(value_type&& value); // C++11
// priority_queue<T, Container, Compare> from <queue>
// It calls c.push_back(value) and then std::push_heap(...).
Parameters
Parameter
Description
value
Required. The element to add. It must have the type T of the queue, or be convertible to it. The queue compares it with the other elements to find its place.
Return Value
Return Value
Description
Nothing
push() returns void. The queue is changed in place, and its size grows by one.
C++ Examples
Example 1 — Push Numbers in Any Order
Push three numbers and then read them back. The output order depends on priority, not on the order of the pushes.
operator< says that one Task is smaller when its priority number is smaller. The queue uses this to decide who is on top.
Each push({...}) builds a Task from the braces and inserts it. The queue sorts them by priority as they arrive.
The loop prints the highest priority first: 9, then 5, then 2. The order of the pushes (2, 9, 5) does not matter.
Frequently Asked Questions
It adds one element to the priority queue and keeps the heap order, so the highest-priority element is always on top. By default the largest value is the top.
No. The element is stored inside a heap and moved to the right place by its priority. Only the top element is easy to see, using top().
push() takes O(log n) time, where n is the number of elements in the queue. This is fast, even for big queues.
Give the type an operator< that compares priority, or pass a custom comparison as the third template argument. Then push() works as it does for numbers.
push() takes a ready-made object. emplace() takes the constructor arguments and builds the object inside the queue, which can avoid an extra copy. Both keep the heap order.