C++ std::is_heap_until() Function

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

Definition

In C++, std::is_heap_until() returns an iterator to the first element that breaks the heap order. Everything before that is a valid heap.

If the whole range is a heap, it returns the end iterator.

Syntax

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

C++
auto it = std::is_heap_until(v.begin(), v.end());
std::is_heap_until(v.begin(), v.end(), std::greater<int>());
CallWhat it does
auto it = std::is_heap_until(v.begin(), v.end());Find where the max-heap rule breaks.
std::is_heap_until(v.begin(), v.end(), std::greater<int>());Check a min-heap.
Library declarations
C++
template<class RandomIt>
RandomIt is_heap_until(RandomIt first, RandomIt last);

template<class RandomIt, class Compare>
RandomIt is_heap_until(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
IteratorPoints at the first element that breaks the heap order.
lastReturned when the whole range is a heap.

C++ Examples

Example 1 — Find the break

A new value was added without push_heap.

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

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

    auto it = std::is_heap_until(v.begin(), v.end());

    std::cout << "Heap part length: " << (it - v.begin()) << '\n';
    return 0;
}

How It Works

  1. The first five values 9, 5, 4, 1 and 3 follow the heap rule.
  2. The last value, 7, sits under 4, but 7 is bigger than 4.
  3. So the rule breaks at index 5.

Example 2 — Whole range is a heap

A heap returns the end iterator.

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

int main() {
    std::vector<int> v = {10, 6, 8};

    auto it = std::is_heap_until(v.begin(), v.end());

    if (it == v.end()) {
        std::cout << "Whole range is a heap\n";
    }
    return 0;
}

How It Works

  1. 10 is bigger than both 6 and 8.
  2. No element breaks the rule.
  3. The function returns v.end().

Example 3 — Min-heap break

Use std::greater to look for a min-heap.

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

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

    auto it = std::is_heap_until(v.begin(), v.end(), std::greater<int>());

    std::cout << "Min-heap part length: " << (it - v.begin()) << '\n';
    return 0;
}

How It Works

  1. In a min-heap, a child must not be smaller than its parent.
  2. 1, 3 and 2 are fine, but the last value 0 is smaller than its parent 3.
  3. The break is at index 3.

Frequently Asked Questions

In C++, std::is_heap_until() finds the first element that breaks the heap rule. Include <algorithm> to use it.
Include <algorithm>. It needs C++11 or newer, so compile with -std=c++11 or later.
It returns an iterator to the first element that breaks the heap rule, or the last iterator if the whole range is a heap.
is_heap() answers true or false. is_heap_until() also tells you where the heap rule breaks.
No. It only reports the position. Call make_heap() to fix the range.

Next: is_partitioned()

Checks whether a range is partitioned by a condition.

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

21 people found this page helpful