Home C++ Algorithms Functions push_heap() C++ std::push_heap() Function 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.
v.push_back(11);
std::push_heap(v.begin(), v.end()); Call What 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 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 Parameter Description first, last Required. The range. The elements before last - 1 must already form a heap. comp Optional. The comparison used to build the heap.
Return Value Return Value Description voidNothing is returned. The range becomes a heap.
C++ Examples 1. Add a larger value 2. Add a smaller value 3. Build a min-heap step by step Example 1 — Add a larger value A new biggest value moves to the front.
#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 make_heap() first makes sure the three values are a heap.11 is added at the end, then push_heap() lets it climb up. It is bigger than everything, so it reaches the front. Example 2 — Add a smaller value A small value does not change the front.
#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 3 is added at the end of the vector. It is smaller than its parent, so it stays low in the heap. 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.
#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 Each push_back() is followed by a push_heap(), so the heap is correct after every step. std::greater<int>() makes it a min-heap.2 is the smallest of 5, 2 and 8, so it ends at the front. Frequently Asked Questions What does std::push_heap() do? In C++, std::push_heap() adds the last element of a range into an existing heap. Include <algorithm> to use it.
Which header and C++ version do I need for std::push_heap()? Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
What does std::push_heap() return? It returns nothing (void). The last element is placed correctly so the range is a heap again.
Do I need to call push_back() first? Yes. push_heap() does not add a value. It only fixes the heap after you added one at the end.
Why use push_heap() instead of std::priority_queue? 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.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion