在线文档教程
C++
算法 | Algorithm

std::is_heap

STD::是[医]堆

Defined in header
template< class RandomIt > bool is_heap( RandomIt first, RandomIt last (1)(since C++11)
template< class ExecutionPolicy, class RandomIt > bool is_heap( ExecutionPolicy&& policy, RandomIt first, RandomIt last (2)(since C++17)
template< class RandomIt, class Compare > bool is_heap( RandomIt first, RandomIt last, Compare comp (3)(since C++11)
template< class ExecutionPolicy, class RandomIt, class Compare > bool is_heap( ExecutionPolicy&& policy, RandomIt first, RandomIt last, Compare comp (4)(since C++17)

检查范围内的元素是否[first, last)最大堆...

1%29个元素的比较operator<...

使用给定的二进制比较函数对3%29个元素进行比较。comp...

policy这些过载不参与过载解决,除非std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

参数

first, last-the range of elements to examine
policy-the execution policy to use. See execution policy for details.
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: bool cmp(const Type1 &a, const Type2 &b The signature does not need to have const &, but the function object must not modify the objects passed to it. 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. ​

类型要求

---它必须符合RandomAccessIterator的要求。

返回值

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

复杂性

直线在之间的距离firstlast...

例外

带有名为ExecutionPolicy报告错误如下:

  • 如果执行作为算法一部分调用的函数,则引发异常ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。

  • 如果算法不能分配内存,std::bad_alloc被扔了。

注记

最大堆是一系列元素[f,l)具有以下属性:

  • 带着N = l - f,为所有人0 < i < N,,,f[floor(i-12)]比不上f[i]...

  • 可以使用std::push_heap()

  • 第一个元素可以使用std::pop_heap()

二次

#include <iostream> #include <algorithm> #include <vector> int main() { std::vector<int> v { 3, 1, 4, 1, 5, 9 }; std::cout << "initially, v: "; for (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: "; for (auto i : v) std::cout << i << ' '; std::cout << '\n'; }

二次

产出:

二次

initially, v: 3 1 4 1 5 9 making heap... after make_heap, v: 9 5 4 1 1 3

二次

另见

is_heap_until (C++11)finds the largest subrange that is a max heap (function template)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/Algorithm/is[医]堆