std:: partition_point
|
헤더 파일에 정의됨
<algorithm>
|
||
|
template
<
class
ForwardIt,
class
UnaryPred
>
ForwardIt partition_point ( ForwardIt first, ForwardIt last, UnaryPred p ) ; |
(C++11부터)
(C++20부터 constexpr) |
|
분할된 범위
[
first
,
last
)
를 검사하고 첫 번째 분할의 끝, 즉
p
를 만족하지 않는 첫 번째 요소를 찾거나, 모든 요소가
p
를 만족할 경우
last
를 반환합니다.
만약
elem
요소들이
[
first
,
last
)
범위 내에서 표현식
bool
(
p
(
elem
)
)
에 대해
분할되지 않은 경우
, 동작은 정의되지 않습니다.
목차 |
매개변수
| first, last | - | 분할된 요소들을 검사할 범위를 정의하는 반복자 쌍 range |
| p | - |
범위의 시작 부분에서 발견된 요소들에 대해
true
를 반환하는 단항 predicate
표현식
p
(
v
)
는
|
| 타입 요구사항 | ||
-
ForwardIt
는
LegacyForwardIterator
요구사항을 충족해야 합니다.
|
||
-
UnaryPred
는
Predicate
요구사항을 충족해야 합니다.
|
||
반환값
[
first
,
last
)
범위 내 첫 번째 파티션의 끝 바로 다음을 가리키는 반복자,
또는 모든 요소가
p
를 만족할 경우
last
.
복잡도
주어진 \(\scriptsize N\) N 이 std:: distance ( first, last ) 인 경우, 술어 p 를 \(\scriptsize O(log(N))\) O(log(N)) 회 적용합니다.
참고 사항
이 알고리즘은
std::lower_bound
의 더 일반화된 형태로,
std::partition_point
와 다음과 같은 조건자를 사용하여 표현할 수 있습니다:
[
&
]
(
const
auto
&
e
)
{
return
e
<
value
;
}
)
;
.
가능한 구현
template<class ForwardIt, class UnaryPred> constexpr //< since C++20 ForwardIt partition_point(ForwardIt first, ForwardIt last, UnaryPred p) { for (auto length = std::distance(first, last); 0 < length; ) { auto half = length / 2; auto middle = std::next(first, half); if (p(*middle)) { first = std::next(middle); length -= (half + 1); } else length = half; } return first; } |
예제
#include <algorithm> #include <array> #include <iostream> #include <iterator> auto print_seq = [](auto rem, auto first, auto last) { for (std::cout << rem; first != last; std::cout << *first++ << ' ') {} std::cout << '\n'; }; int main() { std::array v{1, 2, 3, 4, 5, 6, 7, 8, 9}; auto is_even = [](int i) { return i % 2 == 0; }; std::partition(v.begin(), v.end(), is_even); print_seq("After partitioning, v: ", v.cbegin(), v.cend()); const auto pp = std::partition_point(v.cbegin(), v.cend(), is_even); const auto i = std::distance(v.cbegin(), pp); std::cout << "Partition point is at " << i << "; v[" << i << "] = " << *pp << '\n'; print_seq("First partition (all even elements): ", v.cbegin(), pp); print_seq("Second partition (all odd elements): ", pp, v.cend()); }
가능한 출력:
After partitioning, v: 8 2 6 4 5 3 7 1 9 Partition point is at 4; v[4] = 5 First partition (all even elements): 8 2 6 4 Second partition (all odd elements): 5 3 7 1 9
참고 항목
|
(C++11)
|
특정 조건을 만족하는 첫 번째 요소를 찾음
(function template) |
|
(C++11)
|
범위가 오름차순으로 정렬되었는지 확인
(function template) |
|
주어진 값보다
작지 않은
첫 번째 요소에 대한 반복자를 반환
(function template) |
|
|
(C++20)
|
분할된 범위의 분할 지점을 찾음
(algorithm function object) |