std:: all_of, std:: any_of, std:: none_of
|
ヘッダーで定義
<algorithm>
|
||
|
template
<
class
InputIt,
class
UnaryPred
>
bool all_of ( InputIt first, InputIt last, UnaryPred p ) ; |
(1) |
(C++11以降)
(constexpr C++20以降) |
|
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以降)
(constexpr C++20以降) |
|
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以降)
(constexpr C++20以降) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
UnaryPred
>
bool
none_of
(
ExecutionPolicy
&&
policy,
|
(6) | (C++17以降) |
[
first
,
last
)
内の全ての要素に対して単項述語
p
が
true
を返すかどうかを検査する。
[
first
,
last
)
内の少なくとも1つの要素について、単項述語
p
が
true
を返すかどうかをチェックします。
[
first
,
last
)
内のどの要素に対しても
true
を返さないかどうかをチェックします。
|
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
)
は、
|
| 型要件 | ||
-
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 がスローされます。
実装例
関連実装も参照してください:
-
all_ofは libstdc++ および libc++ に実装されています。 -
any_ofは libstdc++ および libc++ に実装されています。 -
none_ofは libstdc++ および libc++ に実装されています。
| 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
を返すかどうかをチェックする
(アルゴリズム関数オブジェクト) |