std:: is_heap_until
|
定义于头文件
<algorithm>
|
||
|
template
<
class
RandomIt
>
RandomIt is_heap_until ( RandomIt first, RandomIt last ) ; |
(1) |
(C++11 起)
(C++20 起为 constexpr) |
|
template
<
class
ExecutionPolicy,
class
RandomIt
>
RandomIt is_heap_until
(
ExecutionPolicy
&&
policy,
|
(2) | (C++17 起) |
|
template
<
class
RandomIt,
class
Compare
>
RandomIt is_heap_until ( RandomIt first, RandomIt last, Compare comp ) ; |
(3) |
(C++11 起)
(C++20 起为 constexpr) |
|
template
<
class
ExecutionPolicy,
class
RandomIt,
class
Compare
>
RandomIt is_heap_until
(
ExecutionPolicy
&&
policy,
|
(4) | (C++17 起) |
检查范围
[
first
,
last
)
并找出始于
first
的最大子范围,该范围构成一个
堆
。
|
std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> 为 true 。 |
(C++20 前) |
|
std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> 为 true 。 |
(C++20 起) |
目录 |
参数
| first, last | - | 定义待检验元素范围的迭代器对 |
| policy | - | 要使用的执行策略 |
| comp | - |
比较函数对象(即满足比较要求对象),当第一个参数小于第二个参数时返回
true
。
比较函数的签名应等价于: bool cmp ( const Type1 & a, const Type2 & b ) ;
虽然签名不需要包含
const
&
,但函数不得修改传递给它的对象,且必须能够接受所有(可能为 const 的)
|
| 类型要求 | ||
-
RandomIt
必须满足
LegacyRandomAccessIterator
的要求。
|
||
-
Compare
必须满足
Compare
的要求。
|
||
返回值
最后一个迭代器
it
,其对应的范围
[
first
,
it
)
构成一个堆。
复杂度
给定 N 为 std:: distance ( first, last ) :
异常
带有名为
ExecutionPolicy
模板参数的重载按以下方式报告错误:
-
如果作为算法一部分调用的函数执行抛出异常,且
ExecutionPolicy是某个 标准策略 ,则调用 std::terminate 。对于任何其他ExecutionPolicy,行为由实现定义。 - 如果算法无法分配内存,则抛出 std::bad_alloc 。
示例
#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()); // 可能会破坏堆结构 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'; }
输出:
all of v: 9 5 4 1 1 3 2 6 only heap: 9 5 4 1 1 3 2
参见
|
(C++11)
|
检查给定范围是否构成最大堆
(函数模板) |
|
从元素范围创建最大堆
(函数模板) |
|
|
向最大堆添加元素
(函数模板) |
|
|
从最大堆中移除最大元素
(函数模板) |
|
|
将最大堆转换为按升序排序的元素范围
(函数模板) |
|
|
(C++20)
|
查找构成最大堆的最大子范围
(算法函数对象) |