C++ std::priority_queue::push() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
C++98

Definition

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
CallWhat 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

ParameterDescription
valueRequired. 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 ValueDescription
Nothingpush() 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.

C++
#include <iostream>
#include <queue>

int main() {
    std::priority_queue<int> pq;
    pq.push(10);
    pq.push(40);
    pq.push(20);

    std::cout << "Size: " << pq.size() << '\n';
    std::cout << "Order out:";
    while (!pq.empty()) {
        std::cout << ' ' << pq.top();
        pq.pop();
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. Each push() adds one number. After three calls size() reports 3.
  2. The numbers were pushed as 10, 40, 20, but they come back as 40, 20, 10. The queue always gives the largest remaining value first.
  3. The loop calls top() to read and pop() to remove, until empty() says there is nothing left.

Example 2 — Push Strings (Alphabetical Priority)

Strings compare in dictionary order. The word that comes last in the alphabet counts as the biggest.

C++
#include <iostream>
#include <queue>
#include <string>

int main() {
    std::priority_queue<std::string> words;
    words.push("pear");
    words.push("apple");
    words.push("mango");

    std::cout << "Top word: " << words.top() << '\n';
    while (!words.empty()) {
        std::cout << words.top() << '\n';
        words.pop();
    }
    return 0;
}

How It Works

  1. The queue holds std::string values. The text "pear" is converted to a string by push().
  2. The default comparison is std::less<std::string>, which uses dictionary order. "pear" is last in the alphabet, so it is the top.
  3. Draining the queue prints the words from the end of the alphabet to the start: pear, mango, apple.

Example 3 — Push Your Own Type with operator<

For a struct, tell the queue how to compare two items. The item with the higher priority number should count as bigger.

C++
#include <iostream>
#include <queue>
#include <string>

struct Task {
    int priority;
    std::string name;

    bool operator<(const Task& other) const {
        return priority < other.priority;
    }
};

int main() {
    std::priority_queue<Task> tasks;
    tasks.push({2, "Write notes"});
    tasks.push({9, "Fix bug"});
    tasks.push({5, "Review code"});

    while (!tasks.empty()) {
        std::cout << tasks.top().name << " (" << tasks.top().priority << ")\n";
        tasks.pop();
    }
    return 0;
}

How It Works

  1. operator< says that one Task is smaller when its priority number is smaller. The queue uses this to decide who is on top.
  2. Each push({...}) builds a Task from the braces and inserts it. The queue sorts them by priority as they arrive.
  3. 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.

Next: push_range()

Add every element of a range at once (needs C++23) with std::priority_queue.

push_range() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful