is_heap_until() in C++ STL

The C++ Standard Library consists of the header, <algorithm> which defines a collection of functions (principally designed to deal with a range of elements). is_heap_until() is a method in the STL that returns an iterator to the first element in the range which is not in heap order.

Syntax:

is_heap_until( iterator_begin, iterator_end, comparator ); //comparator is optional

Parameters: The is_heap_until() method accepts 3 parameters

  • iterator_begin: Iterator to the initial position of container
  • iterator_end: Iterator to the final position of container ( Note: the range is [iterator_begin,iterator_end) )
  • comparator: Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool.

Return value: iterator to the first element in the range which is not in heap order

Example of is_heap_until() method

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

int main()
{
    vector<int> v;
    v.push_back(4);
    v.push_back(3);
    v.push_back(8);
    v.push_back(-5);
    v.push_back(7);
    v.push_back(2);
    v.push_back(9);

    make_heap(v.begin(), v.begin()+4);
    auto it = is_heap_until(v.begin(), v.end());
    cout<<"The first index that's not part of heap is: "<<distance(v.begin(), it)<<endl;
    cout<<"The first element that's not part of heap is: "<< *it;
}

Output:

The first index that's not part of heap is: 4
The first element that's not part of heap is: 7

This article is written by

Please comment below, if you have any doubts or find any error in the above article.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top