Namespaces
Variants

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

cppreference.net에서 발췌
 
 
알고리즘 라이브러리
제약된 알고리즘 및 범위 기반 알고리즘 (C++20)
제약된 알고리즘, 예: ranges::copy, ranges::sort, ...
정렬 및 관련 연산
분할 연산
(C++11)    

정렬 연산
이진 탐색 연산
(분할된 범위에 대해)
집합 연산 (정렬된 범위에 대해)
병합 연산 (정렬된 범위에 대해)
힙 연산
최소/최대 연산
(C++11)
(C++17)
사전식 비교 연산
순열 연산


 
헤더 <algorithm>
template< class InputIt, class UnaryPred >
bool all_of( InputIt first, InputIt last, UnaryPred p );
에 정의됨 (1) (C++11부터)
(C++20부터 constexpr)
template< class InputIt, class UnaryPred >
bool any_of( InputIt first, InputIt last, UnaryPred p );
(2) (C++11부터)
(C++20부터 constexpr)
template< class InputIt, class UnaryPred >
bool none_of( InputIt first, InputIt last, UnaryPred p );
(3) (C++11부터)
(C++20부터 constexpr)
template< class ExecutionPolicy, class ForwardIt, class UnaryPred >
bool all_of( ExecutionPolicy&& policy,
             ForwardIt first, ForwardIt last, UnaryPred p );
(4) (C++17부터)
template< class ExecutionPolicy, class ForwardIt, class UnaryPred >
bool any_of( ExecutionPolicy&& policy,
             ForwardIt first, ForwardIt last, UnaryPred p );
(5) (C++17부터)
template< class ExecutionPolicy, class ForwardIt, class UnaryPred >
bool none_of( ExecutionPolicy&& policy,
              ForwardIt first, ForwardIt last, UnaryPred p );
(6) (C++17부터)
1) 범위 p 내의 모든 요소에 대해 단항 조건자 true 가 [first, last) 을 반환하는지 확인합니다.
2) 범위 p 내의 하나 이상의 요소에 대해 단항 조건자 true 가 [first, last) 을 반환하는지 확인합니다.
3) 범위 p 내의 어떤 요소에 대해서도 단항 조건자 true 가 [first, last) 을 반환하지 않는지 확인합니다.
4-6) (1-3)과 동일하지만 에 따라 실행됩니다policy.
이러한 오버로드는 다음 표현식의 값이 true 인 경우에만 오버로드 해결에 참여합니다:

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>

(C++20까지)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>

(C++20부터)

매개변수

first, last - 검사할 요소의 범위를 정의하는 반복자 쌍
policy - 사용할 실행 정책
p - 단항 조건자 .

표현식 p(v) 은 모든 인수 bool 에 대해 (const일 수 있는) v 유형으로 변환 가능해야 합니다VT. 여기서 VT 는 InputIt 의 값 유형이며, 값 카테고리에 관계없이 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 적용.

예외

4-6) 실행 과정 중:
  • 병렬화에 필요한 임시 메모리 자원을 사용할 수 없는 경우 std::bad_alloc 이 발생합니다.
  • 알고리즘 인수를 통해 객체에 접근하는 동안 처리되지 않은 예외가 발생하면, 실행 정책에 따라 동작이 결정됩니다 (표준 정책의 경우 std::terminate 가 호출됩니다).

참고 사항

기능 테스트 매크로 값 표준 기능
__cpp_lib_parallel_algorithm 202506L (C++26) 병렬 범위 알고리즘, (4-6)

가능한 구현

의 구현도 참조하십시오

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";
    
    using namespace std::placeholders;
    if (std::none_of(v.cbegin(), v.cend(), std::bind(std::modulus<>(), _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 인지 확인합니다.
(알고리즘 함수 객체)