std::is_partitioned
cppreference.net에서
| 헤더에 정의됨 <algorithm>
|
||
template< class InputIt, class UnaryPred >
bool is_partitioned( InputIt first, InputIt last, UnaryPred p );
|
(1) | (since C++11) (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred >
bool is_partitioned( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, UnaryPred p );
|
(2) | (since C++17) |
1) 다음이 인지 확인합니다
[first, last)가 조건자에 의해 분할되었는지 p: 모든 요소가 p를 만족하면 만족하지 않는 모든 요소보다 먼저 나타납니다.2) 과 동일함(1), 하지만 에 따라 실행됨
policy. 이 오버로드는 다음 표현식의 값이 인 경우에만 오버로드 해결에 참여합니다
true:
|
|
(until C++20) |
|
|
(since C++20) |
매개변수
| first, last | - | 검사할 요소의 범위를 정의하는 반복자 쌍 |
| policy | - | 사용할 실행 정책 |
| p | - | 단항 조건자로, true 범위의 시작 부분에서 발견될 것으로 예상되는 요소에 대해. 표현식 |
| 유형 요구 사항 | ||
-InputIt는 의 요구 사항을 충족해야 합니다 LegacyInputIterator. | ||
-ForwardIt는 의 요구 사항을 충족해야 합니다 LegacyForwardIterator. 그리고 값 유형은 로 변환 가능해야 합니다 UnaryPred 의 매개변수 유형입니다. | ||
-UnaryPred는 의 요구 사항을 충족해야 합니다 Predicate. | ||
반환값
true 만약 요소들 e 의 [first, last)가 분할된 표현식에 대해 p(e). false 그렇지 않습니다.
복잡도
최대 std::distance(first, last) 번의 적용 p.
예외
템플릿 매개변수 이름이 인 오버로드는 ExecutionPolicy 오류를 다음과 같이 보고합니다:
- 알고리즘의 일부로 호출된 함수의 실행이 예외를 발생시키고
ExecutionPolicy이 표준 정책 중 하나인 경우 표준 정책, std::terminate 호출됩니다.ExecutionPolicy다른 모든 - , 동작은 구현 정의입니다.알고리즘이 메모리 할당에 실패하면 std::bad_alloc
이 발생합니다.
template<class InputIt, class UnaryPred>
bool is_partitioned(InputIt first, InputIt last, UnaryPred p)
{
for (; first != last; ++first)
if (!p(*first))
break;
for (; first != last; ++first)
if (p(*first))
return false;
return true;
}
|
가능한 구현
이 코드 실행
#include <algorithm>
#include <array>
#include <iostream>
int main()
{
std::array<int, 9> v {1, 2, 3, 4, 5, 6, 7, 8, 9};
auto is_even = [](int i) { return i % 2 == 0; };
std::cout.setf(std::ios_base::boolalpha);
std::cout << std::is_partitioned(v.begin(), v.end(), is_even) << ' ';
std::partition(v.begin(), v.end(), is_even);
std::cout << std::is_partitioned(v.begin(), v.end(), is_even) << ' ';
std::reverse(v.begin(), v.end());
std::cout << std::is_partitioned(v.cbegin(), v.cend(), is_even) << ' ';
std::cout << std::is_partitioned(v.crbegin(), v.crend(), is_even) << '\n';
}
출력:
false true false true
참고 항목
| 요소 범위를 두 그룹으로 나눕니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |
|
(C++11) |
분할된 범위의 분할 지점을 찾습니다 (함수 템플릿 & 알고리즘 함수 객체) |
(C++20) |
|
(C++20) |
주어진 조건자에 의해 범위가 분할되었는지 확인합니다 (알고리즘 함수 객체) |