std::all_of, std::any_of, std::none_of
cppreference.net에서 발췌
| 헤더 <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 인 경우에만 오버로드 해결에 참여합니다:
|
|
(C++20까지) |
|
|
(C++20부터) |
매개변수
| first, last | - | 검사할 요소의 범위를 정의하는 반복자 쌍 |
| policy | - | 사용할 실행 정책 |
| p | - | 단항 조건자 . 표현식 |
| 유형 요구 사항 | ||
-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-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
참고 항목
(C++20)(C++20)(C++20) |
범위 내의 모든 요소, 일부 요소, 또는 어떤 요소에 대해 조건자가 true 인지 확인합니다.(알고리즘 함수 객체) |