Home » C++ algorithm is_heap_until() function

C++ algorithm is_heap_until() function

by Online Tutorials Library

C++ Algorithm is_heap_until()

C++ Algorithm is_heap_until() function is used to return an iterator pointing at the first element in the range [first, last) that does not satisfy the heap ordering condition, or end if the range forms a heap.

Elements are compared using operator< for the first version or using the given binary comparison function comp for the second version.

Syntax

Parameter

first: A random access iterator pointing to the first element of a range to check for a heap.

last: A random access iterator pointing to the past last element in the range to check for a heap.

comp: A user defined binary predicate function that accepts two arguments and returns true if the two arguments are in order and false otherwise. It follows the strict weak ordering to order the elements.

Return value

It returns an iterator pointing at the first element in the range [first, last) that does not satisfy the heap ordering condition, or end if the range forms a heap.

Complexity

Complexity is linear in the distance between first and last: compares elements until a mismatch is found.

Data races

The objects in the range [first, last) are accessed.

Exceptions

This function throws an exception if either an element comparison or an operation on iterator throws an exception.

Note: The invalid parameters cause an undefined behavior.

Example 1

Let’s see the simple example to demonstrate the use of is_heap_until():

Output:

The 9 first elements are a valid heap: 9 8 7 6 5 4 3 2 1  

Example 2

Let’s see another simple example:

Output:

before: is heap? false   after: is heap? true  

Example 3

Let’s see another simple example:

Output:

d is the first element which does not satisfy the max heap condition.  Entire range is a valid heap.  

Example 4

Let’s see a simple example:

Output:

The container is not heap  The heap elements in container are : 40 30 25  

Next TopicC++ Algorithm

You may also like