std::all_of, std::any_of, std::none_of - cppreference.com
提供: cppreference.com
<tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody>
| ヘッダ |
||
| (1) | ||
|
|
(C++11以上) (C++20未満) |
|
|
|
(C++20以上) | |
|
|
(2) | (C++17以上) |
| (3) | ||
|
|
(C++11以上) (C++20未満) |
|
|
|
(C++20以上) | |
|
|
(4) | (C++17以上) |
| (5) | ||
|
|
(C++11以上) (C++20未満) |
|
|
|
(C++20以上) | |
|
|
(6) | (C++17以上) |
1) 範囲 [first, last) 内のすべての要素に対して単項述語 p が true を返すかどうかを調べます。
3) 範囲 [first, last) 内の少なくともひとつの要素に対して単項述語 p が true を返すかどうかを調べます。
5) 範囲 [first, last) 内のいずれの要素に対しても単項述語 p が true を返さないかどうかを調べます。
2,4,6) (1,3,5) と同じですが、 policy に従って実行されます。 これらのオーバーロードは、 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true でなければ、オーバーロード解決に参加しません。
引数
| first, last | - | 調べる要素の範囲 |
| policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
| p | - | 単項述語。
式 |
| 型の要件 | ||
-InputIt は LegacyInputIterator の要件を満たさなければなりません。
| ||
-ForwardIt は LegacyForwardIterator の要件を満たさなければなりません。
| ||
-UnaryPredicate は Predicate の要件を満たさなければなりません。
| ||
戻り値
1-2) 範囲内のすべての要素に対して単項述語が true を返す場合は true、そうでなければ false。 範囲が空の場合は true を返します。
3-4) 範囲内の少なくともひとつの要素に対して単項述語が true を返す場合は true、そうでなければ false。 範囲が空の場合は false を返します。
5-6) 範囲内のいずれの要素に対しても単項述語が true を返さない場合は true、そうでなければ false。 範囲が空の場合は true を返します。
計算量
1,3,5) 多くとも last - first 回の述語の適用。
2,4,6) O(last-first) 回の述語の適用。
例外
テンプレート引数 ExecutionPolicy を持つオーバーロードは以下のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外を投げ、
ExecutionPolicyが標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆるExecutionPolicyについては、動作は処理系定義です。 - アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。
実装例
libstdc++ と libc++ の all_of の実装も参照してください。
libstdc++ と libc++ の any_of の実装も参照してください。
libstdc++ と libc++ の none_of の実装も参照してください。
| 1つめのバージョン |
|---|
template< class InputIt, class UnaryPredicate > constexpr bool all_of(InputIt first, InputIt last, UnaryPredicate p) { return std::find_if_not(first, last, p) == last; } |
| 2つめのバージョン |
template< class InputIt, class UnaryPredicate > constexpr bool any_of(InputIt first, InputIt last, UnaryPredicate p) { return std::find_if(first, last, p) != last; } |
| 3つめのバージョン |
template< class InputIt, class UnaryPredicate > constexpr bool none_of(InputIt first, InputIt last, UnaryPredicate p) { return std::find_if(first, last, p) == last; } |
例
#include <vector> #include <numeric> #include <algorithm> #include <iterator> #include <iostream> #include <functional> 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<int>(), 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