Cpp-standard-library-cpp-algorithm-is-heap-v2
提供:Dev Guides
C ++アルゴリズムライブラリ-is_heap()関数
説明
- C ++関数* std
- algorithm :: is_heap()*は、指定されたシーケンスが最大ヒープかどうかをテストします。 比較のために `+ binary function +`を使用します。
宣言
- 以下は、std
- algorithmヘッダーからのstd :: algorithm :: is_heap()関数の宣言です。
C 11
template <class RandomAccessIterator, class Compare>
bool is_heap(RandomAccessIterator first,RandomAccessIterator last,Compare comp);
パラメーター
- first -初期位置へのランダムアクセス反復子。
- last -最終位置へのランダムアクセス反復子。
- comp -2つの引数を受け取り、boolを返すバイナリ関数。
戻り値
指定されたシーケンスが最大ヒープの場合はtrueを返し、そうでない場合はfalseを返します。
例外
要素比較またはイテレータの操作が例外をスローした場合、例外をスローします。
無効なパラメータは未定義の動作を引き起こすことに注意してください。
時間の複雑さ
リニア。
例
- 次の例は、std
- algorithm :: is_heap()関数の使用法を示しています。
#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'};
bool result;
result = is_heap(v.begin(), v.end());
if (result == false)
cout << "Given sequence is not a max heap." << endl;
result = is_heap(v.begin(), v.end(), ignore_case);
if (result == true)
cout << "Given sequence is a max heap." << endl;
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Given sequence is not a max heap.
Given sequence is a max heap.