Cpp-standard-library-cpp-algorithm-all-of
提供:Dev Guides
C ++アルゴリズムライブラリ-all_of()関数
説明
- C ++関数* std
- algorithm :: all_of()*は、 `+ predicate `が ` first `から ` last +`の範囲のすべての要素に対してtrueを返す場合にtrueを返します。 範囲が空の場合はtrueも返され、そうでない場合はfalseが返されます。
宣言
- 以下は、std
- algorithmヘッダーからのstd :: algorithm :: all_of()関数の宣言です。
C 11
template <class InputIterator, class UnaryPredicate>
bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred);
パラメーター
- first -シーケンスの初期位置に反復子を入力します。
- last -シーケンスの最後の位置に反復子を入力します。
- pred -要素を受け取り、 `+ bool +`を返す単項述語関数。
戻り値
範囲内のすべての要素について述語がtrueを返す場合、または範囲が空の場合はtrueを返し、そうでない場合はfalseを返します。
例外
述語またはイテレータの操作が例外をスローした場合、例外をスローします。
無効なパラメータは未定義の動作を引き起こすことに注意してください。
時間の複雑さ
`+ first `と ` last +`の間の距離は線形です。
例
- 次の例は、std
- algorithm :: all_of()関数の使用法を示しています。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool is_even(int n) {
return (n % 2 == 0);
}
int main(void) {
vector<int> v = {2, 4, 6, 8, 10};
bool result;
result = all_of(v.begin(), v.end(), is_even);
if (result == true)
cout << "Vector contains all even numbers." << endl;
v[0] = 1;
result = all_of(v.begin(), v.end(), is_even);
if (result == false)
cout << "Vector doesn't contain all even numbers." << endl;
return 0;
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Vector contains all even numbers.
Vector doesn't contain all even numbers.