std::is_heap_until
| Defined in header <algorithm>
|
||
template< class RandomIt >
RandomIt is_heap_until( RandomIt first, RandomIt last );
|
(1) | (since C++11) |
template< class RandomIt, class Compare >
RandomIt is_heap_until( RandomIt first, RandomIt last, Compare comp );
|
(2) | (since C++11) |
template< class ExecutionPolicy, class RandomIt >
RandomIt is_heap_until( ExecutionPolicy&& policy,
RandomIt first, RandomIt last );
|
(3) | (since C++17) |
template< class ExecutionPolicy, class RandomIt, class Compare >
RandomIt is_heap_until( ExecutionPolicy&& policy,
RandomIt first, RandomIt last, Compare comp );
|
(4) | (since C++17) |
Examines the target range [first, last) and finds the largest range which begins at first and represents a heap.
operator<(until C++20)std::less{}(since C++20).comp.policy.true:
|
|
(until C++20) |
|
|
(since C++20) |
Parameters
| first, last | - | the pair of iterators defining the target range |
| comp | - | comparison function object (i.e. an object that satisfies the requirements of Compare) which returns true if the first argument is less than the second.The signature of the comparison function should be equivalent to the following:
While the signature does not need to have |
| policy | - | the execution policy to use |
| Type requirements | ||
-RandomIt must meet the requirements of LegacyRandomAccessIterator.
| ||
-Compare must meet the requirements of Compare.
| ||
Return value
The past-the-end iterator of the largest range found.
Complexity
Given N as std::distance(first, last):
operator<(until C++20)std::less{}(since C++20).comp.Exceptions
- If the temporary memory resources required for parallelization are not available, std::bad_alloc is thrown.
- If an uncaught exception is thrown while accessing objects via an algorithm argument, the behavior is determined by the execution policy (for standard policies, std::terminate is invoked).
Example
import std;
void print_tree(const char* remark, std::span<const int> tree)
{
std::size_t w = (1 << (std::bit_width(tree.size()) - 1)) * 4;
std::println("{:^{}}", remark, w);
for (std::size_t n{}; n != tree.size(); ++n)
if (std::print("{:^{}}", tree[n], w); std::has_single_bit(n + 2))
w /= 2, std::println();
std::println();
}
int main()
{
std::vector tree{3, 1, 4, 1, 5};
std::make_heap(tree.begin(), tree.end());
// Mess up the heap
tree.push_back(9);
tree.push_back(2);
const auto it_heap_end = std::is_heap_until(tree.begin(), tree.end());
const std::span heap(tree.begin(), it_heap_end);
print_tree("Full tree:", tree);
print_tree("Heap:", heap);
}
Output:
Full tree:
5
3 4
1 1 9 2
Heap:
5
3 4
1 1
See also
(C++20) |
finds the largest subrange that is a max heap (algorithm function object) |
(C++11) |
checks if the given range is a max heap (function template & algorithm function object) |
(C++20) |
|
| creates a max heap out of a range of elements (function template & algorithm function object) | |
(C++20) |
|
| adds an element to a max heap (function template & algorithm function object) | |
(C++20) |
|
| removes the largest element from a max heap (function template & algorithm function object) | |
(C++20) |
|
| turns a max heap into a range of elements sorted in ascending order (function template & algorithm function object) | |
(C++20) |