all_any_none_of

all_any_none_of (조건을 모두/하나라도/전혀 만족하는지 검사)

범위의 원소들이 단항 술어를 얼마나 만족하는지 묻는 세 가지 알고리즘을 한데 모은 페이지예요. all_of, any_of, none_of가 각각 "모두", "적어도 하나","하나도" 만족 여부를 알려줘요. <algorithm> 헤더에 있어요.

출처: cppreference

본문

세 함수 모두 범위 [first, last)의 원소 각각에 단항 술어 p를 적용해요.

template< class InputIt, class UnaryPred >
bool all_of( InputIt first, InputIt last, UnaryPred p );   // (1)

template< class InputIt, class UnaryPred >
bool any_of( InputIt first, InputIt last, UnaryPred p );   // (2)

template< class InputIt, class UnaryPred >
bool none_of( InputIt first, InputIt last, UnaryPred p );  // (3)

C++17부터 실행 정책을 받는 오버로드가 추가됐어요.

    1. all_of: 범위의 모든 원소에 대해 p가 참이면 true를 반환해요.
    1. any_of: 적어도 하나의 원소에 대해 p가 참이면 true를 반환해요.
    1. none_of: 어떤 원소에 대해서도 p가 참이 아니면 true를 반환해요.

특이한 점은 범위가 비어 있을 때예요. all_ofnone_of는 빈 범위에서 true를, any_offalse를 반환해요. "전제가 성립하지 않으면 참"이라는 논리 규칙을 그대로 따른 거죠.

어떤 동작이 실행 정책 오버로드에 참여하려면 ExecutionPolicy가 유효한 실행 정책이어야 해요(C++20 이전엔 std::decay_t<ExecutionPolicy>, 이후엔 std::remove_cvref_t<ExecutionPolicy>).

all_of는 "전부 체크하고 통과", any_of는 "하나라도 발견", none_of는 "하나도 없는지"를 확인할 때 각각 쓰면 자연스러워요.

더 알아보기 (Learn more)

cppreference