C++ std::make_heap() Function

Definition
In C++, std::make_heap() turns a range into a heap. By default it is a max-heap, so the biggest element sits at the front.
A heap is not fully sorted, but it lets you get the largest (or smallest) value quickly. It is the base of std::priority_queue.
Syntax
Add #include <algorithm>, then call std::make_heap(). Start with these two lines.
std::make_heap(v.begin(), v.end());
std::make_heap(v.begin(), v.end(), std::greater<int>());
| Call | What it does |
|---|
std::make_heap(v.begin(), v.end()); | Build a max-heap. The largest value goes to v.front(). |
std::make_heap(v.begin(), v.end(), std::greater<int>()); | Build a min-heap. The smallest value goes to v.front(). |
Library declarations
template<class RandomIt>
void make_heap(RandomIt first, RandomIt last);
template<class RandomIt, class Compare>
void make_heap(RandomIt first, RandomIt last, Compare comp);
Parameters
| Parameter | Description |
|---|
| first, last | Required. The range to turn into a heap. It needs random access iterators. |
| comp | Optional. A comparison function. Use std::greater for a min-heap. |
Return Value
| Return Value | Description |
|---|
void | Nothing is returned. The range is rearranged in place. |
C++ Examples
Example 1 — Max-heap
Find the largest value with a heap.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {3, 1, 4, 1, 5, 9, 2};
std::make_heap(v.begin(), v.end());
std::cout << "Largest: " << v.front() << '\n';
return 0;
}
How It Works
make_heap() rearranges the values so the biggest one comes first.- The other elements are in a heap order, not sorted.
v.front() reads the largest value, 9.
Example 2 — Min-heap
Use std::greater to get the smallest value first.
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {3, 1, 4, 1, 5, 9, 2};
std::make_heap(v.begin(), v.end(), std::greater<int>());
std::cout << "Smallest: " << v.front() << '\n';
return 0;
}
How It Works
std::greater<int>() reverses the idea of "bigger".- Now the smallest value is placed at the front.
v.front() is 1.
Example 3 — Check with is_heap
A vector becomes a heap after make_heap.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {2, 7, 4};
std::cout << std::boolalpha;
std::cout << "Before: " << std::is_heap(v.begin(), v.end()) << '\n';
std::make_heap(v.begin(), v.end());
std::cout << "After: " << std::is_heap(v.begin(), v.end()) << '\n';
return 0;
}
Before: false
After: true
How It Works
- In a max-heap the first value must not be smaller than its children.
- 2 is smaller than 7, so the vector is not a heap at first.
- After
make_heap() the check passes.
Frequently Asked Questions
In C++, std::make_heap() rearranges a range into a 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 range is rearranged into a heap, with the largest element first by default.
No. Only the first element is guaranteed to be the largest (or smallest). Use sort_heap() to fully sort a heap.
Pass std::greater<int>() as the third argument to make_heap().
Next: max()
Returns the larger of two values, or the largest in a list.
max() tutorial →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.
18 people found this page helpful