Namespaces
Variants

std::ranges:: all_of, std::ranges:: any_of, std::ranges:: 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
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
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
(注:このHTML要素には翻訳対象のテキストコンテンツが含まれていないため、元の構造を保持したまま出力します)
定義済みヘッダー <algorithm>
呼び出しシグネチャ
template < std:: input_iterator I, std:: sentinel_for < I > S,

class Proj = std:: identity ,
std:: indirect_unary_predicate < std :: projected < I, Proj >> Pred >

constexpr bool all_of ( I first, S last, Pred pred, Proj proj = { } ) ;
(1) (C++20以降)
template < ranges:: input_range R, class Proj = std:: identity ,

std:: indirect_unary_predicate <
std :: projected < ranges:: iterator_t < R > ,Proj >> Pred >

constexpr bool all_of ( R && r, Pred pred, Proj proj = { } ) ;
(2) (C++20以降)
template < std:: input_iterator I, std:: sentinel_for < I > S,

class Proj = std:: identity ,
std:: indirect_unary_predicate < std :: projected < I, Proj >> Pred >

constexpr bool any_of ( I first, S last, Pred pred, Proj proj = { } ) ;
(3) (C++20以降)
template < ranges:: input_range R, class Proj = std:: identity ,

std:: indirect_unary_predicate <
std :: projected < ranges:: iterator_t < R > ,Proj >> Pred >

constexpr bool any_of ( R && r, Pred pred, Proj proj = { } ) ;
(4) (C++20以降)
template < std:: input_iterator I, std:: sentinel_for < I > S,

class Proj = std:: identity ,
std:: indirect_unary_predicate < std :: projected < I, Proj >> Pred >

constexpr bool none_of ( I first, S last, Pred pred, Proj proj = { } ) ;
(5) (C++20以降)
template < ranges:: input_range R, class Proj = std:: identity ,

std:: indirect_unary_predicate <
std :: projected < ranges:: iterator_t < R > ,Proj >> Pred >

constexpr bool none_of ( R && r, Pred pred, Proj proj = { } ) ;
(6) (C++20以降)
1) 単項述語 pred が範囲 [ first , last ) 内の少なくとも1つの要素に対して(射影 proj で射影した後) false を返すかどうかをチェックします。
3) 単項述語 pred が範囲 [ first , last ) 内の少なくとも1つの要素に対して(射影 proj で射影した後) true を返すかどうかをチェックします。
5) 単項述語 pred が範囲 [ first , last ) 内の要素のいずれに対しても(射影 proj を適用後) true を返さないかどうかをチェックします。
2,4,6) (1,3,5) と同じですが、 r をソース範囲として使用します。これは、 ranges:: begin ( r ) first として、 ranges:: end ( r ) last として使用する場合と同様です。

このページで説明されている関数ライクなエンティティは、 アルゴリズム関数オブジェクト (非公式には niebloids として知られる)です。すなわち:

目次

パラメータ

first, last - 検査対象の要素の 範囲 を定義するイテレータ-番兵ペア
r - 検査対象の要素の範囲
pred - 投影された要素に適用する述語
proj - 要素に適用する投影

戻り値

1,2) true が返される条件:範囲内のすべてのイテレータ i に対して std:: invoke ( pred, std:: invoke ( proj, * i ) ) ! = false が成立する場合。それ以外の場合は false を返す。範囲が空の場合は true を返す。
3,4) true が返される条件: std:: invoke ( pred, std:: invoke ( proj, * i ) ) ! = false が範囲内の少なくとも1つのイテレータ i に対して成り立つ場合。それ以外の場合は false を返す。範囲が空の場合は false を返す。
5,6) true が返される条件: std:: invoke ( pred, std:: invoke ( proj, * i ) ) == false が範囲内の全てのイテレータ i に対して成り立つ場合。 false が返される場合: それ以外の場合。範囲が空の場合は true を返す。
範囲に true の要素がある はい いいえ
範囲に false の要素がある はい いいえ はい いいえ [1]
all_of false true false true
any_of true true false false
none_of false false true true
  1. この場合、範囲は空です。

計算量

最大で last - first 回の述語と射影の適用。

実装例

**注記**: このC++コードは技術的な内容であり、HTMLタグ、属性、コードブロック内のテキストは翻訳対象外としました。C++のキーワード、型名、関数名などの専門用語も原文のまま保持しています。 このC++コードは翻訳対象外のため、HTMLタグとコードブロックをそのまま保持しています。コード内のC++固有の用語や構造はすべて元のままです。 **翻訳結果:** **説明:** - HTMLタグと属性はそのまま保持されています - C++関数名「none_of」は翻訳対象外のため原文のまま - 括弧内の数字(5,6)もコード関連のため翻訳せず - この構造では翻訳すべき自然言語テキストが含まれていないため、全体が原文のままとなります **注記**: 提供されたHTMLコードはC++のコードスニペットを含んでおり、指示に従って以下の点を厳密に守りました: - HTMLタグと属性は一切翻訳せず - ` `, `
`, ``タグ内のテキストは翻訳せず
- C++固有の用語(`struct`, `template`, `constexpr`など)は翻訳せず
- 元のフォーマットを完全に保持
コード部分は全て原文のまま保持されています。
all_of (1,2)
struct all_of_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
             std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
    constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const
    {
        return ranges::find_if_not(first, last, std::ref(pred), std::ref(proj)) == last;
    }
    template<ranges::input_range R, class Proj = std::identity,
             std::indirect_unary_predicate<
                 std::projected<ranges::iterator_t<R>,Proj>> Pred>
    constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const
    {
        return operator()(ranges::begin(r), ranges::end(r),
                          std::ref(pred), std::ref(proj));
    }
};
inline constexpr all_of_fn all_of;
any_of (3,4)
struct any_of_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
             std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
    constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const
    {
        return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) != last;
    }
    template<ranges::input_range R, class Proj = std::identity,
             std::indirect_unary_predicate<
                 std::projected<ranges::iterator_t<R>,Proj>> Pred>
    constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const
    {
        return operator()(ranges::begin(r), ranges::end(r),
                          std::ref(pred), std::ref(proj));
    }
};
inline constexpr any_of_fn any_of;
none_of (5,6)
none_of (5,6)
struct none_of_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
             std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
    constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const
    {
        return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) == last;
    }
    template<ranges::input_range R, class Proj = std::identity,
             std::indirect_unary_predicate<
                 std::projected<ranges::iterator_t<R>,Proj>> Pred>
    constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const
    {
        return operator()(ranges::begin(r), ranges::end(r),
                          std::ref(pred), std::ref(proj));
    }
};
inline constexpr none_of_fn none_of;

#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
namespace ranges = std::ranges;
constexpr bool some_of(auto&& r, auto&& pred) // some but not all
{
    return not (ranges::all_of(r, pred) or ranges::none_of(r, pred));
}
constexpr auto w = {1, 2, 3};
static_assert(!some_of(w, [](int x) { return x < 1; }));
static_assert( some_of(w, [](int x) { return x < 2; }));
static_assert(!some_of(w, [](int x) { return x < 4; }));
int main()
{
    std::vector<int> v(10, 2);
    std::partial_sum(v.cbegin(), v.cend(), v.begin());
    std::cout << "Among the numbers: ";
    ranges::copy(v, std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
    if (ranges::all_of(v.cbegin(), v.cend(), [](int i) { return i % 2 == 0; }))
        std::cout << "All numbers are even\n";
    if (ranges::none_of(v, std::bind(std::modulus<int>(), std::placeholders::_1, 2)))
        std::cout << "None of them are odd\n";
    auto DivisibleBy = [](int d)
    {
        return [d](int m) { return m % d == 0; };
    };
    if (ranges::any_of(v, 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++11) (C++11) (C++11)
述語が範囲内のすべての要素、いずれかの要素、またはどの要素に対しても true であるかどうかをチェックする
(関数テンプレート)