C++ std::make_heap() Function

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

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.

C++
std::make_heap(v.begin(), v.end());
std::make_heap(v.begin(), v.end(), std::greater<int>());
CallWhat 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
C++
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

ParameterDescription
first, lastRequired. The range to turn into a heap. It needs random access iterators.
compOptional. A comparison function. Use std::greater for a min-heap.

Return Value

Return ValueDescription
voidNothing is returned. The range is rearranged in place.

C++ Examples

Example 1 — Max-heap

Find the largest value with a heap.

C++
#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

  1. make_heap() rearranges the values so the biggest one comes first.
  2. The other elements are in a heap order, not sorted.
  3. v.front() reads the largest value, 9.

Example 2 — Min-heap

Use std::greater to get the smallest value first.

C++
#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

  1. std::greater<int>() reverses the idea of "bigger".
  2. Now the smallest value is placed at the front.
  3. v.front() is 1.

Example 3 — Check with is_heap

A vector becomes a heap after make_heap.

C++
#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;
}

How It Works

  1. In a max-heap the first value must not be smaller than its children.
  2. 2 is smaller than 7, so the vector is not a heap at first.
  3. 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

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.

18 people found this page helpful