std::is_heap_until(3)
π std::is_heap_until β C++ Standard Libary, page std::is_heap_until(3) π
okay so bestie, this is the std::is_heap_until page from the C++ Standard Libary (yes, βLibaryβ is literally in the manual name, typo and all π). itβs an algorithm page, itβs giving heap detective energy, and weβre gonna get through every overload together. π«Ά
πΈ NAME πΈ
std::is_heap_until β and yeah, the name is std::is_heap_until, iconic repetition, no notes π
.
β¨ Synopsis β¨
Defined in header <algorithm>.
template< class RandomIt > // (1) (since C++11)
RandomIt is_heap_until( RandomIt first, RandomIt last ); // (constexpr since C++20)
template< class ExecutionPolicy, class RandomIt >
RandomIt is_heap_until( ExecutionPolicy&& policy, // (2) (since C++17)
RandomIt first, RandomIt last );
template< class RandomIt, class Compare > // (since C++11)
RandomIt is_heap_until( RandomIt first, RandomIt last, // (3)
Compare comp ); // (constexpr since C++20)
template< class ExecutionPolicy, class RandomIt, class Compare >
RandomIt is_heap_until( ExecutionPolicy&& policy, // (4) (since C++17)
RandomIt first, RandomIt last,
Compare comp );
okay so what does it actually do? it examines the range [first, last) and finds the largest range beginning at first which is a heap. very demure, very mindful, very heap-aware πΈ
- the heap property being checked is with respect to
operator<(until C++20) orstd::less{}(since C++20). - same as (1), but executed according to
policy. - the heap property being checked is with respect to
comp. - same as (3), but executed according to
policy.
and for overloads (2) and (4): these overloads participate in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true (until C++20), or if std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> is true (since C++20). periodt π«Ά
π Parameters π
first,lastβ the range of elements to examine. thatβs the zone weβre checking, bestie β¨policyβ the execution policy to use. see execution policy for details πcompβ the comparison function object (i.e. an object that satisfies the requirements ofCompare) which returnstrueif the first argument is less than the second.
the signature of the comparison function should be equivalent to the following:
bool cmp(const Type1& a, const Type2& b);
while the signature does not need to have const&, the function must not modify the objects passed to it and must be able to accept all values of type (possibly const) Type1 and Type2 regardless of value category (thus, Type1& is not allowed, nor is Type1 unless for Type1 a move is equivalent to a copy (since C++11)).
the types Type1 and Type2 must be such that an object of type RandomIt can be dereferenced and then implicitly converted to both of them. yes, both. no cap π₯
π Type requirements π
RandomItmust meet the requirements ofLegacyRandomAccessIterator.Comparemust meet the requirements ofCompare.
π Return value π
the last iterator it for which range [first, it) is a heap. thatβs the cutoff, bestie βοΈβ¨
π Complexity π
given \(\scriptsize N\)N as std::distance(first, last):
- 1,2)
\(\scriptsize O(N)\)O(N)comparisons usingoperator<(until C++20) orstd::less{}(since C++20). - 3,4)
\(\scriptsize O(N)\)O(N)applications of the comparison functioncomp.
π«Ά Exceptions π«Ά
the overloads with a template parameter named ExecutionPolicy report errors as follows:
- if execution of a function invoked as part of the algorithm throws an exception and
ExecutionPolicyis one of the standard policies,std::terminateis called. for any otherExecutionPolicy, the behavior is implementation-defined. π - if the algorithm fails to allocate memory,
std::bad_allocis thrown.
okay this part is serious: those exception rules are not a vibe, theyβre facts. handle with care π
π¦ Example π¦
// Run this code
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v{3, 1, 4, 1, 5, 9};
std::make_heap(v.begin(), v.end());
// probably mess up the heap
v.push_back(2);
v.push_back(6);
auto heap_end = std::is_heap_until(v.begin(), v.end());
std::cout << "all of v: ";
for (const auto& i : v)
std::cout << i << ' ';
std::cout << '\n';
std::cout << "only heap: ";
for (auto i = v.begin(); i != heap_end; ++i)
std::cout << *i << ' ';
std::cout << '\n';
}
Output:
all of v: 9 5 4 1 1 3 2 6
only heap: 9 5 4 1 1 3 2
β See also β
is_heapβ checks if the given range is a max heap (C++11) (function template)make_heapβ creates a max heap out of a range of elements (function template)push_heapβ adds an element to a max heap (function template)pop_heapβ removes the largest element from a max heap (function template)sort_heapβ turns a max heap into a range of elements sorted in ascending order (function template)ranges::is_heap_untilβ finds the largest subrange that is a max heap (C++20) (niebloid)
mwah π thatβs the whole page, bestie. source: http://cppreference.com; version/date: 2024.06.10; manual page: std::is_heap_until(3). byeee π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms