名前空間
変種
操作

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

From cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
制約付きアルゴリズムとRangeアルゴリズム (C++20)
制約付きアルゴリズム、例: ranges::copy, ranges::sort, ...
実行ポリシー (C++17)
シーケンスを変更しない操作
一括操作
(C++17)
検索操作
all_ofany_ofnone_of
(C++11)                (C++11)(C++11)

シーケンスを変更する操作
コピー操作
(C++11)
(C++11)
スワップ操作
変換操作
生成操作
削除操作
順序変更操作
(C++17まで)(C++11)
(C++20)(C++20)
サンプリング操作
(C++17)

ソートおよび関連操作
パーティション操作
ソート操作
二分探索操作
(パーティション化された範囲)
集合操作 (ソート済み範囲)
マージ操作 (ソート済み範囲)
ヒープ操作
最小/最大操作
(C++11)
(C++17)
辞書順比較操作
順列操作
Cライブラリ
数値演算
未初期化メモリに対する操作
 
ヘッダー <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,

             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以降)
(C++20 以降 constexpr)
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以降)
(C++20 以降 constexpr)
template< class ExecutionPolicy, class ForwardIt, class UnaryPred >

bool none_of( ExecutionPolicy&& policy,

              ForwardIt first, ForwardIt last, UnaryPred p );
(6) (C++17以降)
1) 単項述語 p が範囲 [firstlast) 内の少なくとも1つの要素に対して false を返すかどうかをチェックします。
3) 単項述語 p が範囲 [firstlast) 内の少なくとも1つの要素に対して true を返すかどうかをチェックします。
5) 単項述語 p が範囲 [firstlast) 内のどの要素に対しても 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 も含む) VT の各引数 v に対して bool に変換可能でなければなりません。ここで VTInputIt の値型であり、値カテゴリに関係なく、v を変更してはなりません。したがって、VT& のパラメーター型は許可されません。また、VT のムーブがコピーと同等でない限り、VT も許可されません(C++11以降)

型要件
-
InputItLegacyInputIterator の要件を満たす必要があります。
-
ForwardItLegacyForwardIterator の要件を満たさなければなりません。
-
UnaryPredPredicate の要件を満たさなければなりません。

[編集] 戻り値

true の要素があります はい いいえ
 false の要素があります  はい いいえ はい     いいえ[1]
all_of false true false true
any_of true true   false     false  
none_of   false     false   true true
  1. この場合、範囲は空です。

[編集] 計算量

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

[編集] 例外

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

[編集] 関連項目

範囲内のすべての、いずれかの、またはどの要素も述語が true にならないかをチェックする
(アルゴリズム関数オブジェクト)[編集]
English 日本語 中文(简体) 中文(繁體)