Cpp-standard-library-cpp-algorithm-is-heap-until-v2
提供:Dev Guides
C ++アルゴリズムライブラリ-is_heap_until()関数
説明
- C ++関数* std
- algorithm :: is_heap_until()*は、最大ヒープ条件に違反するシーケンスから最初の要素を見つけます。 比較のために `+ binary function +`を使用します。
宣言
- 以下は、std
- algorithmヘッダーからのstd :: algorithm :: is_heap_until()関数の宣言です。
C 11
template <class RandomAccessIterator>
template <class RandomAccessIterator, class Compare>
RandomAccessIterator is_heap_until(RandomAccessIterator first,
RandomAccessIterator last Compare comp);
パラメーター
- first -初期位置へのランダムアクセス反復子。
- last -最終位置へのランダムアクセス反復子。
- comp -2つの引数を受け取り、boolを返すバイナリ関数。
戻り値
最大ヒープ条件に違反する最初の要素へのイテレータを返します。 シーケンス全体が有効な最大ヒープである場合、 `+ last +`を返します。
例外
要素比較またはイテレータの操作が例外をスローした場合、例外をスローします。
無効なパラメータは未定義の動作を引き起こすことに注意してください。
時間の複雑さ
リニア。
例
- 次の例は、std
- algorithm :: is_heap_until()関数の使用法を示しています。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool ignore_case(char a, char b) {
return (tolower(a) == tolower(b));
}
int main(void) {
vector<char> v = {'E', 'd', 'C', 'b', 'A'};
auto result = is_heap_until(v.begin(), v.end());
cout << char(*result) << " is the first element which "
<< "violates the max heap." << endl;
v = {5, 4, 3, 2, 1};
result = is_heap_until(v.begin(), v.end(), ignore_case);
if (result == end(v))
cout << "Entire range is valid heap." << endl;
return 0;
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
d is the first element which violates the max heap.
Entire range is valid heap.