std:: all_of, std:: any_of, std:: none_of
|
헤더 파일에 정의됨
<algorithm>
|
||
|
template
<
class
InputIt,
class
UnaryPred
>
bool all_of ( InputIt first, InputIt last, UnaryPred p ) ; |
(1) |
(C++11부터)
(C++20부터 constexpr) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
UnaryPred
>
bool
all_of
(
ExecutionPolicy
&&
policy,
|
(2) | (C++17부터) |
|
template
<
class
InputIt,
class
UnaryPred
>
bool any_of ( InputIt first, InputIt last, UnaryPred p ) ; |
(3) |
(C++11부터)
(C++20부터 constexpr) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
UnaryPred
>
bool
any_of
(
ExecutionPolicy
&&
policy,
|
(4) | (C++17부터) |
|
template
<
class
InputIt,
class
UnaryPred
>
bool none_of ( InputIt first, InputIt last, UnaryPred p ) ; |
(5) |
(C++11부터)
(C++20부터 constexpr) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
UnaryPred
>
bool
none_of
(
ExecutionPolicy
&&
policy,
|
(6) | (C++17부터) |
[
first
,
last
)
내의 모든 요소에 대해 단항 술어
p
가
true
를 반환하는지 확인합니다.
[
first
,
last
)
내의 최소한 하나의 요소에 대해
true
를 반환하는지 확인합니다.
[
first
,
last
)
내의 어떤 요소에 대해서도 단항 술어
p
가
true
를 반환하지 않는지 확인합니다.
|
std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> 가 true 인 경우. |
(C++20 이전) |
|
std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> 가 true 인 경우. |
(C++20 이후) |
목차 |
매개변수
| first, last | - | 검사할 요소들의 범위 를 정의하는 반복자 쌍 |
| policy | - | 사용할 실행 정책 |
| p | - |
단항 predicate.
표현식
p
(
v
)
는
|
| 타입 요구사항 | ||
-
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 |
- ↑ 이 경우 범위는 비어 있습니다.
복잡도
예외
ExecutionPolicy
라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:
-
알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고
ExecutionPolicy가 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른ExecutionPolicy의 경우 동작은 구현에 따라 정의됩니다. - 알고리즘이 메모리 할당에 실패할 경우, std::bad_alloc 이 throw됩니다.
가능한 구현
참고 구현:
| 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"; if (std::none_of(v.cbegin(), v.cend(), std::bind(std::modulus<>(), std::placeholders::_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
인지, 일부 요소에 대해 true인지, 또는 어떤 요소에도 true가 아닌지 검사합니다
(알고리즘 함수 객체) |