Namespaces
Variants

std:: all_of, std:: any_of, std:: none_of

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
all_of any_of none_of
(C++11) (C++11) (C++11)

Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
ヘッダーで定義 <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,

ForwardIt first, ForwardIt last, UnaryPred p ) ;
(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,

ForwardIt first, ForwardIt last, UnaryPred p ) ;
(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,

ForwardIt first, ForwardIt last, UnaryPred p ) ;
(6) (C++17以降)
1) 範囲 [ first , last ) 内の全ての要素に対して単項述語 p true を返すかどうかを検査する。
3) 範囲 [ first , last ) 内の少なくとも1つの要素について、単項述語 p 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以降)

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - C++関連の専門用語(Parameters、Return value、Complexity、Exceptions、Possible implementation、Example、See also)は原文のまま保持しました - HTMLタグ、属性、クラス名、ID、リンク先は一切変更していません - 数値や書式設定は完全に保持されています

パラメータ

first, last - 検査する要素の範囲を定義するイテレータのペア
policy - 使用する実行ポリシー
p - 単項述語

p ( v ) は、 VT 型(const修飾されている可能性もある)のすべての引数 v に対して bool に変換可能でなければならず、 値カテゴリ に関わらず、 v を変更してはならない。したがって、 VT & のパラメータ型は許可されない 。また、 VT も、 VT においてムーブがコピーと等価である場合を除いて許可されない (C++11以降) 。 ​

型要件
-
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. この場合、範囲は空です。

計算量

1-6) 最大で std:: distance ( first, last ) 回の述語 p の適用。

例外

ExecutionPolicy という名前のテンプレートパラメータを持つオーバーロードは、 以下のようにエラーを報告します:

  • アルゴリズムの一部として呼び出された関数の実行が例外をスローした場合、 ExecutionPolicy 標準ポリシー のいずれかであるとき、 std::terminate が呼び出されます。それ以外の ExecutionPolicy については、動作は実装定義です。
  • アルゴリズムがメモリの確保に失敗した場合、 std::bad_alloc がスローされます。

実装例

関連実装も参照してください:

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

関連項目

範囲内の全ての要素、いずれかの要素、またはいずれの要素に対しても述語が true を返すかどうかをチェックする
(アルゴリズム関数オブジェクト)