std::is_heap
| Defined in header <algorithm>
|
||
template< class RandomIt >
bool is_heap( RandomIt first, RandomIt last );
|
(1) | (since C++11) (constexpr since C++20) |
template< class RandomIt, class Compare >
bool is_heap( RandomIt first, RandomIt last, Compare comp );
|
(2) | (since C++11) (constexpr since C++20) |
template< class ExecutionPolicy, class RandomIt >
bool is_heap( ExecutionPolicy&& policy,
RandomIt first, RandomIt last );
|
(3) | (since C++17) |
template< class ExecutionPolicy, class RandomIt, class Compare >
bool is_heap( ExecutionPolicy&& policy,
RandomIt first, RandomIt last, Compare comp );
|
(4) | (since C++17) |
Checks whether the target range [first, last) 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
true if the target range represents a heap with respect to the corresponding comparator, false otherwise.
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
#include <algorithm>
#include <bit>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9};
std::cout << "Initially, v:\n";
for (const auto& i : v)
std::cout << i << ' ';
std::cout << '\n';
if (!std::is_heap(v.begin(), v.end()))
{
std::cout << "Making heap...\n";
std::make_heap(v.begin(), v.end());
}
std::cout << "After make_heap, v:\n";
for (auto t{1U}; const auto& i : v)
std::cout << i << (std::has_single_bit(++t) ? " | " : " ");
std::cout << '\n';
}
Output:
Initially, v:
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9
Making heap...
After make_heap, v:
9 | 6 9 | 5 5 9 7 | 1 1 3 5 8 3 4 2 |
See also
(C++20) |
checks if the given range is a max heap (algorithm function object) |
(C++11) |
finds the largest subrange that 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) |