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
헤더에 정의됨 <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 ) 내 요소 중 적어도 하나에 대해 (투영 proj 를 적용한 후) false 를 반환하는지 확인합니다.
3) 단항 조건자 pred 가 범위 [ first , last ) 내의 적어도 하나의 요소에 대해 (투영 proj 를 적용한 후) true 를 반환하는지 확인합니다.
5) 단항 조건자 pred 가 범위 [ first , last ) 내의 어떤 요소에 대해서도 (투영 proj 를 적용한 후) true 를 반환하지 않는지 확인합니다.
2,4,6) (1,3,5) 와 동일하지만, r 를 소스 범위로 사용하며, 마치 ranges:: begin ( r ) first 로, ranges:: end ( r ) last 로 사용하는 것과 같습니다.

이 페이지에서 설명하는 함수형 개체들은 algorithm function objects (일반적으로 niebloids 로 알려진)입니다. 즉:

목차

매개변수

first, last - 검사할 요소들의 범위 를 정의하는 반복자-감시자 쌍
r - 검사할 요소들의 범위
pred - 투영된 요소들에 적용할 조건자
proj - 요소들에 적용할 투영

반환값

1,2) true 를 반환합니다 - 범위 내 모든 반복자 i 에 대해 std:: invoke ( pred, std:: invoke ( proj, * i ) ) ! = false 인 경우. 그렇지 않으면 false 를 반환합니다. 범위가 비어 있는 경우 true 를 반환합니다.
3,4) true 를 반환합니다 - 범위 내의 적어도 하나의 반복자 i 에 대해 std:: invoke ( pred, std:: invoke ( proj, * i ) ) ! = false 인 경우. 그렇지 않으면 false 를 반환합니다. 범위가 비어 있으면 false 를 반환합니다.
5,6) true 를 반환합니다 - 범위 내 모든 반복자 i 에 대해 std:: invoke ( pred, std:: invoke ( proj, * i ) ) == false 인 경우. 그렇지 않으면 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 번의 술어(predicate)와 투영(projection) 적용.

가능한 구현

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)
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) // 일부만 해당 (전체는 아님)
{
    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 << "숫자들: ";
    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 << "모든 숫자가 짝수입니다\n";
    if (ranges::none_of(v, std::bind(std::modulus<int>(), std::placeholders::_1, 2)))
        std::cout << "어떤 숫자도 홀수가 아닙니다\n";
    auto DivisibleBy = [](int d)
    {
        return [d](int m) { return m % d == 0; };
    };
    if (ranges::any_of(v, DivisibleBy(7)))
        std::cout << "적어도 하나의 숫자가 7로 나누어집니다\n";
}

출력:

숫자들: 2 4 6 8 10 12 14 16 18 20
모든 숫자가 짝수입니다
어떤 숫자도 홀수가 아닙니다
적어도 하나의 숫자가 7로 나누어집니다

참고 항목

(C++11) (C++11) (C++11)
범위 내의 모든 요소에 대해 조건자가 true 인지, 일부 요소에 대해 true인지, 또는 어떤 요소에도 true가 아닌지 검사합니다
(함수 템플릿)