std::all_of, std::any_of, std::none_of
ja.cppreference.net より
| Defined in header <algorithm>
|
||
template< class InputIt, class UnaryPred >
bool all_of( InputIt first, InputIt last, UnaryPred p );
|
(1) | (since C++11) (constexpr since C++20) |
template< class InputIt, class UnaryPred >
bool any_of( InputIt first, InputIt last, UnaryPred p );
|
(2) | (since C++11) (constexpr since C++20) |
template< class InputIt, class UnaryPred >
bool none_of( InputIt first, InputIt last, UnaryPred p );
|
(3) | (since C++11) (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred >
bool all_of( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, UnaryPred p );
|
(4) | (since C++17) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred >
bool any_of( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, UnaryPred p );
|
(5) | (since C++17) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred >
bool none_of( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, UnaryPred p );
|
(6) | (since C++17) |
1) 単項述語
p が true true を返すかどうかを、範囲 [first, last) 内のすべての要素について調べます。2) 単項述語
p が true true を返すかどうかを、範囲 [first, last) 内の少なくとも1つの要素について調べます。3) 単項述語
p が true true を返すかどうかを、範囲 [first, last) 内のどの要素についても調べます。4-6) (1-3) と同じですが、(1-3) に従って実行されます
policy。 これらのオーバーロードは、次の式の値が
true 真である場合にのみオーバーロード解決に参加します:
|
|
(until C++20) |
|
|
(since C++20) |
引数
| first, last | - | 調査する要素の範囲を定義するイテレータのペア 範囲 の調査する要素 |
| policy | - | 使用する 実行ポリシー |
| p | - | 単項述語 . 式 |
| 型要件 | ||
-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
|
- ↑ この場合、範囲は空です。
計算量
1-6) 最大でも
std::distance(first, last) 回の p の適用。例外
4-6) 実行処理中:
- 並列化に必要な一時メモリリソースが利用できない場合、std::bad_alloc がスローされます。
- アルゴリズム引数を介してオブジェクトにアクセス中にキャッチされない例外がスローされた場合、動作は実行ポリシーによって決まります (標準ポリシー、std::terminate が呼び出されます)。
ノート
| 機能テスト マクロ 値 標準 機能 | (C++26) | 並列範囲アルゴリズム、 | ( |
|---|---|---|---|
__cpp_lib_parallel_algorithm |
202506L |
4-6 | )実装例以下の実装も参照してください 内の |
libstdc++
および
template<class InputIt, class UnaryPred>
constexpr bool all_of(InputIt first, InputIt last, UnaryPred p)
{
return std::find_if_not(first, last, p) == last;
}
|
| none_of |
template<class InputIt, class UnaryPred>
constexpr bool any_of(InputIt first, InputIt last, UnaryPred p)
{
return std::find_if(first, last, p) != last;
}
|
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";
using namespace std::placeholders;
if (std::none_of(v.cbegin(), v.cend(), std::bind(std::modulus<>(), _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 であるかどうかを、範囲内のすべて、いずれか、またはどの要素に対しても調べます(アルゴリズム関数オブジェクト) |