C++ std::push_heap() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

Definition

In C++, std::push_heap() assumes everything except the last element is already a heap. It moves the last element into the right place so the whole range is a heap again.

The usual pattern is to call push_back() on the vector first, then push_heap().

Syntax

Add #include <algorithm>, then call std::push_heap(). Start with these two lines.

C++
v.push_back(11);
std::push_heap(v.begin(), v.end());
CallWhat it does
v.push_back(11);Add the new value at the end.
std::push_heap(v.begin(), v.end());Move it into its place in the heap.
Library declarations
C++
template<class RandomIt>
void push_heap(RandomIt first, RandomIt last);

template<class RandomIt, class Compare>
void push_heap(RandomIt first, RandomIt last, Compare comp);

Parameters

ParameterDescription
first, lastRequired. The range. The elements before last - 1 must already form a heap.
compOptional. The comparison used to build the heap.

Return Value

Return ValueDescription
voidNothing is returned. The range becomes a heap.

C++ Examples

Example 1 — Add a larger value

A new biggest value moves to the front.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {9, 5, 7};
    std::make_heap(v.begin(), v.end());

    v.push_back(11);
    std::push_heap(v.begin(), v.end());

    std::cout << "Largest: " << v.front() << '\n';
    return 0;
}

How It Works

  1. make_heap() first makes sure the three values are a heap.
  2. 11 is added at the end, then push_heap() lets it climb up.
  3. It is bigger than everything, so it reaches the front.

Example 2 — Add a smaller value

A small value does not change the front.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {9, 5, 7};
    std::make_heap(v.begin(), v.end());

    v.push_back(3);
    std::push_heap(v.begin(), v.end());

    std::cout << "Largest: " << v.front() << '\n';
    std::cout << "Size: " << v.size() << '\n';
    return 0;
}

How It Works

  1. 3 is added at the end of the vector.
  2. It is smaller than its parent, so it stays low in the heap.
  3. The front is still 9, and the size is 4.

Example 3 — Build a min-heap step by step

Push values one at a time with std::greater.

C++
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v;

    v.push_back(5);
    std::push_heap(v.begin(), v.end(), std::greater<int>());
    v.push_back(2);
    std::push_heap(v.begin(), v.end(), std::greater<int>());
    v.push_back(8);
    std::push_heap(v.begin(), v.end(), std::greater<int>());

    std::cout << "Smallest: " << v.front() << '\n';
    return 0;
}

How It Works

  1. Each push_back() is followed by a push_heap(), so the heap is correct after every step.
  2. std::greater<int>() makes it a min-heap.
  3. 2 is the smallest of 5, 2 and 8, so it ends at the front.

Frequently Asked Questions

In C++, std::push_heap() adds the last element of a range into an existing heap. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns nothing (void). The last element is placed correctly so the range is a heap again.
Yes. push_heap() does not add a value. It only fixes the heap after you added one at the end.
push_heap() works on a normal vector you already have, so you can still look at or change the data. priority_queue wraps all this for you.

Next: random_shuffle()

Randomly reorders a range using rand() (removed in C++17).

random_shuffle() 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.

20 people found this page helpful