跳到主要内容

std::is_heap() 算法

// (1)
template< class RandomIt >
constexpr bool is_heap( RandomIt first, RandomIt last );

// (2)
template< class RandomIt, class Compare >
constexpr bool is_heap( RandomIt first, RandomIt last, Compare comp );

// (3)
template< class ExecutionPolicy, class RandomIt >
bool is_heap( ExecutionPolicy&& policy, RandomIt first, RandomIt last );

// (4)
template< class ExecutionPolicy, class RandomIt, class Compare >
bool is_heap( ExecutionPolicy&& policy, RandomIt first, RandomIt last, Compare comp );

检查范围 [first; last) 中的元素是否构成最大堆

  • (1) 元素使用 operator< 进行比较。

  • (2) 元素使用给定的二元比较函数 comp 进行比较。

  • (3 - 4)(1 - 2) 相同,但根据 policy 执行。

    重载决议

    这些重载仅在 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (C++20之前) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (C++20起)true 时参与重载决议。

参数

first
last

要检查的元素范围。

policy

要使用的执行策略。有关详细信息,请参阅执行策略

comp

比较函数对象(即满足 Compare 要求的对象)。比较函数的签名应等同于以下内容:

bool cmp(const Type1 &a, const Type2 &b);
  • 签名不需要有 `const&`,但不得修改参数。
  • 必须接受 (可能为 const) TypeType2 类型的所有值,无论其值类别如何(因此不允许 Type1&也不允许 Type1,除非对于 Type1,移动等同于复制 (C++11起)
  • `Type1` 和 `Type2` 类型必须是 `RandomIt` 类型的对象可以隐式转换为它们两者的类型。

类型要求

RandomItLegacyRandomAccessIterator

返回值

如果范围是最大堆,则为 true,否则为 false

复杂度

firstlast 之间的距离成线性关系。

异常

带有模板参数 ExecutionPolicy 的重载报告错误如下

  • 如果作为算法一部分调用的函数执行时抛出异常,并且 ExecutionPolicy标准策略之一,则调用std::terminate。对于任何其他 ExecutionPolicy,行为是实现定义的.
  • 如果算法未能分配内存,则抛出 std::bad_alloc

备注

最大堆是具有以下属性的元素范围 [f; l)

  • 给定 Nl - f,对于所有 0 < i < Nf[(i - 1) / 2] 不与 f[i] 比较小。
  • 可以使用 std::push_heapO(log(N)) 时间内添加新元素。
  • 可以使用 std::pop_heapO(log(N)) 时间内移除第一个元素。

示例

Main.cpp
#include <algorithm>
#include <array>
#include <iostream>

int main()
{
const auto v = {1, 2, 3, 4};

for (int n : {3, 5})
(std::is_heap(v.begin(), v.end(), n) == std::end(v))
? std::cout << "v does not contain " << n << '\n'
: std::cout << "v contains " << n << '\n';

auto is_even = [](int i) { return i % 2 == 0; };

for (auto const& w : {std::array{3, 1, 4}, {1, 3, 5}})
if (auto it = std::is_heap_if(begin(w), end(w), is_even); it != std::end(w))
std::cout << "w contains an even number " << *it << '\n';
else
std::cout << "w does not contain even numbers\n";
}
输出
v contains 3
v does not contain 5
w contains an even number 4
w does not contain even numbers
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::is_heap() 算法

// (1)
template< class RandomIt >
constexpr bool is_heap( RandomIt first, RandomIt last );

// (2)
template< class RandomIt, class Compare >
constexpr bool is_heap( RandomIt first, RandomIt last, Compare comp );

// (3)
template< class ExecutionPolicy, class RandomIt >
bool is_heap( ExecutionPolicy&& policy, RandomIt first, RandomIt last );

// (4)
template< class ExecutionPolicy, class RandomIt, class Compare >
bool is_heap( ExecutionPolicy&& policy, RandomIt first, RandomIt last, Compare comp );

检查范围 [first; last) 中的元素是否构成最大堆

  • (1) 元素使用 operator< 进行比较。

  • (2) 元素使用给定的二元比较函数 comp 进行比较。

  • (3 - 4)(1 - 2) 相同,但根据 policy 执行。

    重载决议

    这些重载仅在 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (C++20之前) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (C++20起)true 时参与重载决议。

参数

first
last

要检查的元素范围。

policy

要使用的执行策略。有关详细信息,请参阅执行策略

comp

比较函数对象(即满足 Compare 要求的对象)。比较函数的签名应等同于以下内容:

bool cmp(const Type1 &a, const Type2 &b);
  • 签名不需要有 `const&`,但不得修改参数。
  • 必须接受 (可能为 const) TypeType2 类型的所有值,无论其值类别如何(因此不允许 Type1&也不允许 Type1,除非对于 Type1,移动等同于复制 (C++11起)
  • `Type1` 和 `Type2` 类型必须是 `RandomIt` 类型的对象可以隐式转换为它们两者的类型。

类型要求

RandomItLegacyRandomAccessIterator

返回值

如果范围是最大堆,则为 true,否则为 false

复杂度

firstlast 之间的距离成线性关系。

异常

带有模板参数 ExecutionPolicy 的重载报告错误如下

  • 如果作为算法一部分调用的函数执行时抛出异常,并且 ExecutionPolicy标准策略之一,则调用std::terminate。对于任何其他 ExecutionPolicy,行为是实现定义的.
  • 如果算法未能分配内存,则抛出 std::bad_alloc

备注

最大堆是具有以下属性的元素范围 [f; l)

  • 给定 Nl - f,对于所有 0 < i < Nf[(i - 1) / 2] 不与 f[i] 比较小。
  • 可以使用 std::push_heapO(log(N)) 时间内添加新元素。
  • 可以使用 std::pop_heapO(log(N)) 时间内移除第一个元素。

示例

Main.cpp
#include <algorithm>
#include <array>
#include <iostream>

int main()
{
const auto v = {1, 2, 3, 4};

for (int n : {3, 5})
(std::is_heap(v.begin(), v.end(), n) == std::end(v))
? std::cout << "v does not contain " << n << '\n'
: std::cout << "v contains " << n << '\n';

auto is_even = [](int i) { return i % 2 == 0; };

for (auto const& w : {std::array{3, 1, 4}, {1, 3, 5}})
if (auto it = std::is_heap_if(begin(w), end(w), is_even); it != std::end(w))
std::cout << "w contains an even number " << *it << '\n';
else
std::cout << "w does not contain even numbers\n";
}
输出
v contains 3
v does not contain 5
w contains an even number 4
w does not contain even numbers
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。