std::is_heap
提供: cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| ヘッダ <algorithm> で定義
|
||
| (1) | ||
template< class RandomIt > bool is_heap( RandomIt first, RandomIt last ); |
(C++11以上) (C++20未満) |
|
template< class RandomIt > constexpr bool is_heap( RandomIt first, RandomIt last ); |
(C++20以上) | |
template< class ExecutionPolicy, class RandomIt > bool is_heap( ExecutionPolicy&& policy, RandomIt first, RandomIt last ); |
(2) | (C++17以上) |
| (3) | ||
template< class RandomIt, class Compare > bool is_heap( RandomIt first, RandomIt last, Compare comp ); |
(C++11以上) (C++20未満) |
|
template< class RandomIt, class Compare > constexpr bool is_heap( RandomIt first, RandomIt last, Compare comp ); |
(C++20以上) | |
template< class ExecutionPolicy, class RandomIt, class Compare > bool is_heap( ExecutionPolicy&& policy, RandomIt first, RandomIt last, Compare comp ); |
(4) | (C++17以上) |
範囲 [first, last) 内の要素が最大ヒープであるかどうか調べます。
1) 要素は
operator< を用いて比較されます。3) 要素は指定された二項比較関数
comp を用いて比較されます。2,4) (1,3) と同じですが、
policy に従って実行されます。 これらのオーバーロードは、 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true でなければ、オーバーロード解決に参加しません。引数
| first, last | - | 調べる要素の範囲 |
| policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
| comp | - | 第1引数が第2引数より小さい場合に true を返す、比較関数オブジェクト (Compare の要件を満たすオブジェクト)。比較関数のシグネチャは以下と同等であるべきです。
シグネチャが |
| 型の要件 | ||
-RandomIt は LegacyRandomAccessIterator の要件を満たさなければなりません。
| ||
戻り値
範囲が最大ヒープであれば true、そうでなければ false。
計算量
first と last の距離に比例。
例外
テンプレート引数 ExecutionPolicy を持つオーバーロードは以下のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外を投げ、
ExecutionPolicyが標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆるExecutionPolicyについては、動作は処理系定義です。 - アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。
ノート
最大ヒープは以下の性質を持つ要素の範囲 [f,l) です。
N = l - fとしたとき、すべての0 < i < Nについてf[floor(が
)]i-1 2 f[i]より小さくない。std::push_heap()を使用して新しい要素を追加できる。std::pop_heap()を使用して最初の要素を削除できる。
例
Run this code
#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
関連項目
(C++11) |
最大ヒープである最も大きな部分範囲を探します (関数テンプレート) |