std::is_heap_until
提供: cppreference.com
ヘッダ <algorithm> で定義 | ||
(1) | ||
template<class RandomIt > RandomIt is_heap_until( RandomIt first, RandomIt last ); | (C++11およびそれ以降) (C++20以前) | |
template<class RandomIt > constexpr RandomIt is_heap_until( RandomIt first, RandomIt last ); | (C++20およびそれ以降) | |
template<class ExecutionPolicy, class RandomIt > RandomIt is_heap_until( ExecutionPolicy&& policy, RandomIt first, RandomIt last ); | (2) | (C++17およびそれ以降) |
(3) | ||
template<class RandomIt, class Compare > RandomIt is_heap_until( RandomIt first, RandomIt last, Compare comp ); | (C++11およびそれ以降) (C++20以前) | |
template<class RandomIt, class Compare > constexpr RandomIt is_heap_until( RandomIt first, RandomIt last, Compare comp ); | (C++20およびそれ以降) | |
template<class ExecutionPolicy, class RandomIt, class Compare > RandomIt is_heap_until( ExecutionPolicy&& policy, RandomIt first, RandomIt last, Compare comp ); | (4) | (C++17およびそれ以降) |
範囲 [first, last)
を調べ、最大ヒープである最も大きな first
から始まる範囲を探します。
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 の要件を満たすオブジェクト)。比較関数のシグネチャは以下と同等であるべきです。 bool cmp(const Type1 &a, const Type2 &b); シグネチャが const& を持つ必要はありませんが、関数オブジェクトは渡されたオブジェクトを変更してはなりません。 |
型の要件 | ||
-RandomIt は RandomAccessIterator の要件を満たさなければなりません。 |
[編集]戻り値
最大ヒープである最も大きな first
から始まる範囲の上限。 つまり、範囲 [first, it)
が最大ヒープである最後のイテレータ it
。
[編集]計算量
first
と last
の距離に比例。
[編集]例外
テンプレート引数 ExecutionPolicy
を持つオーバーロードは以下のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外を投げ、
ExecutionPolicy
が3つの標準のポリシーのいずれかの場合は、 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::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(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) | 指定範囲が最大ヒープであるかどうか調べます (関数テンプレート) |