名前空間
変種
操作

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

提供: cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
制約付きアルゴリズムと範囲に対するアルゴリズム (C++20)
コンセプトとユーティリティ: std::sortable, std::projected, ...
制約付きアルゴリズム: std::ranges::copy, std::ranges::sort, ...
実行ポリシー (C++17)
非変更シーケンス操作
all_ofany_ofnone_of
(C++11)(C++11)(C++11)
(C++17)
変更シーケンス操作
未初期化記憶域の操作
分割操作
ソート操作
(C++11)
二分探索操作
集合操作 (ソート済み範囲用)
ヒープ操作
(C++11)
最小/最大演算
(C++11)
(C++17)

順列
数値演算
C のライブラリ
 
ヘッダ <algorithm> で定義
(1)
template< class InputIt, class UnaryPredicate >
bool all_of( InputIt first, InputIt last, UnaryPredicate p );
(C++11以上)
(C++20未満)
template< class InputIt, class UnaryPredicate >
constexpr bool all_of( InputIt first, InputIt last, UnaryPredicate p );
(C++20以上)
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
bool all_of( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPredicate p );
(2) (C++17以上)
(3)
template< class InputIt, class UnaryPredicate >
bool any_of( InputIt first, InputIt last, UnaryPredicate p );
(C++11以上)
(C++20未満)
template< class InputIt, class UnaryPredicate >
constexpr bool any_of( InputIt first, InputIt last, UnaryPredicate p );
(C++20以上)
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
bool any_of( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPredicate p );
(4) (C++17以上)
(5)
template< class InputIt, class UnaryPredicate >
bool none_of( InputIt first, InputIt last, UnaryPredicate p );
(C++11以上)
(C++20未満)
template< class InputIt, class UnaryPredicate >
constexpr bool none_of( InputIt first, InputIt last, UnaryPredicate p );
(C++20以上)
template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate >
bool none_of( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPredicate p );
(6) (C++17以上)
1) 範囲 [first, last) 内のすべての要素に対して単項述語 ptrue を返すかどうかを調べます。
3) 範囲 [first, last) 内の少なくともひとつの要素に対して単項述語 ptrue を返すかどうかを調べます。
5) 範囲 [first, last) 内のいずれの要素に対しても単項述語 ptrue を返さないかどうかを調べます。
2,4,6) (1,3,5) と同じですが、 policy に従って実行されます。 これらのオーバーロードは、 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true でなければ、オーバーロード解決に参加しません。

目次

[編集] 引数

first, last - 調べる要素の範囲
policy - 使用する実行ポリシー。 詳細は実行ポリシーを参照してください
p - 単項述語。

p(v)VT 型 (およびその const 修飾された型) のすべての引数 v について、その値カテゴリにかかわらず、 bool に変換可能でなければなりません。 ただし VTInputIt の値型です。 また、 v を変更してはなりません。 そのため、引数の型 VT& は許されません。 また、 VT に対してムーブがコピーと同等でなければ VT も許されません。 (C++11以上) ​​

型���要件
-
InputItLegacyInputIterator の要件を満たさなければなりません。
-
ForwardItLegacyForwardIterator の要件を満たさなければなりません。
-
UnaryPredicatePredicate の要件を満たさなければなりません。

[編集] 戻り値

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

[編集] 関連項目

指定範囲内の要素のすべて、1個以上、または0個に対して述語が true を返すかどうか調べます
(ニーブロイド) [edit]