C++ std::is_heap() Function

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

Definition

In C++, std::is_heap() tells you whether a range is already a valid heap. By default it checks for a max-heap.

It is a quick check to run before calling pop_heap() or sort_heap().

Syntax

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

C++
std::is_heap(v.begin(), v.end());
std::is_heap(v.begin(), v.end(), std::greater<int>());
CallWhat it does
std::is_heap(v.begin(), v.end());Is this a max-heap?
std::is_heap(v.begin(), v.end(), std::greater<int>());Is this a min-heap?
Library declarations
C++
template<class RandomIt>
bool is_heap(RandomIt first, RandomIt last);

template<class RandomIt, class Compare>
bool is_heap(RandomIt first, RandomIt last, Compare comp);

Parameters

ParameterDescription
first, lastRequired. The range to check.
compOptional. The comparison used for the heap.

Return Value

Return ValueDescription
trueThe range is a heap (an empty range counts too).
falseThe heap rule is broken somewhere.

C++ Examples

Example 1 — A valid max-heap

Check a vector that follows the heap rule.

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

int main() {
    std::vector<int> v = {9, 5, 4, 1, 3};

    bool ok = std::is_heap(v.begin(), v.end());

    std::cout << std::boolalpha << "Heap: " << ok << '\n';
    return 0;
}

How It Works

  1. In a max-heap every value is at least as big as its children.
  2. 9 is above 5 and 4, and 5 is above 1 and 3.
  3. All checks pass, so the answer is true.

Example 2 — Not a heap

A smaller parent breaks the rule.

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

int main() {
    std::vector<int> v = {1, 5, 4};

    bool ok = std::is_heap(v.begin(), v.end());

    std::cout << std::boolalpha << "Heap: " << ok << '\n';
    return 0;
}

How It Works

  1. The first value, 1, is the parent of 5 and 4.
  2. A max-heap parent must not be smaller than its children.
  3. 1 is smaller than 5, so the result is false.

Example 3 — Check a min-heap

Use std::greater to test a min-heap.

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

int main() {
    std::vector<int> v = {1, 3, 2, 7, 5};

    std::cout << std::boolalpha;
    std::cout << "Min-heap: " << std::is_heap(v.begin(), v.end(), std::greater<int>()) << '\n';
    std::cout << "Max-heap: " << std::is_heap(v.begin(), v.end()) << '\n';
    return 0;
}

How It Works

  1. For a min-heap, every parent must not be bigger than its children.
  2. 1 is under all, 3 is under 7 and 5, and 2 has no children.
  3. The same data is not a max-heap, because 1 is smaller than 3.

Frequently Asked Questions

In C++, std::is_heap() checks whether a range is a heap. Include <algorithm> to use it.
Include <algorithm>. It needs C++11 or newer, so compile with -std=c++11 or later.
It returns true if the range is a heap with respect to the comparison, and false otherwise.
Yes. is_heap() returns true for an empty range.
C++11.

Next: is_heap_until()

Finds the first element that breaks the heap rule.

is_heap_until() 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