is_partitioned

is_partitioned (분할 범위인지 검사)

범위가 주어진 술어 기준으로 "참인 원소들 → 거짓인 원소들" 순서로 분할되어 있는지 검사하는 알고리즘이에요. <algorithm> 헤더에 있어요.

출처: cppreference

본문

is_partitioned는 범위 [first, last)가 술어 p 기준으로 분할(partitioned)되어 있는지 검사해요.

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

분할되었다는 것은, p가 참인 모든 원소가 p가 거짓인 모든 원소보다 앞에 온다는 뜻이에요. 즉 참 그룹이 먼저, 거짓 그룹이 나중에 정렬된 상태예요.

  • 반환 값: [first, last)가 비어 있거나 p에 대해 분할되어 있으면 true.
  • 복잡도: 최대 last - first번의 p 호출.
std::vector<int> v{4, 2, 6, 1, 3};   // 짝수 먼저, 홀수 나중 (분할됨)
bool ok = std::is_partitioned(v.begin(), v.end(),
                              [](int x){ return x % 2 == 0; });   // true

std::partition으로 분할한 결과가 올바른지, 또는 분할된 전제를 요구하는 알고리즘(partition_point 등)을 쓰기 전에 확인할 때 유용해요.

더 알아보기 (Learn more)

cppreference