std::all_of, std::any_of, std::none_of
From cppreference.com
| ヘッダー <algorithm> で定義 |
||
template< class InputIt, class UnaryPred > bool all_of( InputIt first, InputIt last, UnaryPred p ); |
(1) | (C++11以降) (C++20 以降 constexpr) |
| template< class ExecutionPolicy, class ForwardIt, class UnaryPred > bool all_of( ExecutionPolicy&& policy, |
(2) | (C++17以降) |
template< class InputIt, class UnaryPred > bool any_of( InputIt first, InputIt last, UnaryPred p ); |
(3) | (C++11以降) (C++20 以降 constexpr) |
| template< class ExecutionPolicy, class ForwardIt, class UnaryPred > bool any_of( ExecutionPolicy&& policy, |
(4) | (C++17以降) |
template< class InputIt, class UnaryPred > bool none_of( InputIt first, InputIt last, UnaryPred p ); |
(5) | (C++11以降) (C++20 以降 constexpr) |
| template< class ExecutionPolicy, class ForwardIt, class UnaryPred > bool none_of( ExecutionPolicy&& policy, |
(6) | (C++17以降) |
1) 単項述語 p が範囲
[first, last) 内の少なくとも1つの要素に対して false を返すかどうかをチェックします。3) 単項述語 p が範囲
[first, last) 内の少なくとも1つの要素に対して true を返すかどうかをチェックします。5) 単項述語 p が範囲
[first, last) 内のどの要素に対しても true を返さないかどうかをチェックします。2,4,6) (1,3,5) と同じですが、policy に従って実行されます。
これらのオーバーロードは、以下のすべての条件が満たされた場合にのみオーバーロード解決に参加する。
|
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である。 |
(C++20まで) |
|
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> は true です。 |
(C++20以降) |
目次 |
[編集] パラメーター
| first, last | - | 検査する要素の範囲を定義するイテレータのペア |
| policy | - | 使用する 実行ポリシー |
| p | - | 単項述語です。 式 p(v) は、型 (const も含む) |
| 型要件 | ||
-InputIt は LegacyInputIterator の要件を満たす必要があります。 | ||
-ForwardIt は LegacyForwardIterator の要件を満たさなければなりません。 | ||
-UnaryPred は Predicate の要件を満たさなければなりません。 | ||
[編集] 戻り値
| true の要素があります | はい | いいえ | ||
|---|---|---|---|---|
| false の要素があります | はい | いいえ | はい | いいえ[1] |
all_of
|
false | true | false | true |
any_of
|
true | true | false | false |
none_of
|
false | false | true | true |
- ↑ この場合、範囲は空です。
[編集] 計算量
[編集] 例外
ExecutionPolicy というテンプレートパラメータを持つオーバーロードは、次のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外をスローし、
ExecutionPolicyが 標準ポリシー のいずれかである場合、std::terminate が呼び出されます。その他のExecutionPolicyの場合、動作は実装定義です。 - アルゴリズムがメモリの割り当てに失敗した場合、std::bad_alloc がスローされます。
[編集] 可能な実装
以下の実装も参照してください
- libstdc++ と libc++ における
all_ofの実装。 - libstdc++ と libc++ における
any_ofの実装。 - libstdc++ と libc++ における
none_ofの実装。
| all_of |
|---|
template<class InputIt, class UnaryPred> constexpr bool all_of(InputIt first, InputIt last, UnaryPred p) { return std::find_if_not(first, last, p) == last; } |
| any_of |
template<class InputIt, class UnaryPred> constexpr bool any_of(InputIt first, InputIt last, UnaryPred p) { return std::find_if(first, last, p) != last; } |
| none_of |
template<class InputIt, class UnaryPred> constexpr bool none_of(InputIt first, InputIt last, UnaryPred p) { return std::find_if(first, last, p) == last; } |
[編集] 例
このコードを実行
#include <algorithm> #include <functional> #include <iostream> #include <iterator> #include <numeric> #include <vector> int main() { std::vector<int> v(10, 2); std::partial_sum(v.cbegin(), v.cend(), v.begin()); std::cout << "Among the numbers: "; std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; if (std::all_of(v.cbegin(), v.cend(), [](int i) { return i % 2 == 0; })) std::cout << "All numbers are even\n"; if (std::none_of(v.cbegin(), v.cend(), std::bind(std::modulus<>(), std::placeholders::_1, 2))) std::cout << "None of them are odd\n"; struct DivisibleBy { const int d; DivisibleBy(int n) : d(n) {} bool operator()(int n) const { return n % d == 0; } }; if (std::any_of(v.cbegin(), v.cend(), DivisibleBy(7))) std::cout << "At least one number is divisible by 7\n"; }
出力
Among the numbers: 2 4 6 8 10 12 14 16 18 20 All numbers are even None of them are odd At least one number is divisible by 7
[編集] 関連項目
| (C++20)(C++20)(C++20) |
範囲内のすべての、いずれかの、またはどの要素も述語が true にならないかをチェックする (アルゴリズム関数オブジェクト) |