std::partition
| 헤더 <algorithm>
|
||
template< class ForwardIt, class UnaryPred >
ForwardIt partition( ForwardIt first, ForwardIt last, UnaryPred p );
|
에 정의됨 (1) | (constexpr C++20부터) |
template< class ExecutionPolicy, class ForwardIt, class UnaryPred >
ForwardIt partition( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, UnaryPred p );
|
(2) | (C++17부터) |
[first, last) 내의 요소들을 재배열하여, 술어 p가 true을 반환하는 모든 요소들이 술어 p가 false을 반환하는 모든 요소들 앞에 오도록 합니다. 요소들의 상대적 순서는 유지되지 않습니다. policy에 따라 실행됩니다.true인 경우에만 오버로드 해석에 참여합니다:
|
|
(C++20까지) |
|
|
(C++20부터) |
만약 *first의 유형이 Swappable(C++11까지)ForwardIt이 아니거나 ValueSwappable(C++11부터)가 아닌 경우, 동작은 정의되지 않습니다.
매개변수
| first, last | - | 재정렬할 요소들의 범위 를 정의하는 반복자 쌍 |
| policy | - | 사용할 실행 정책 |
| p | - |
요소가 다른 요소들보다 먼저 정렬되어야 하는 경우
true
를 반환하는 단항 predicate
표현식
p
(
v
)
는
|
| 타입 요구사항 | ||
-
ForwardIt
는
LegacyForwardIterator
요구사항을 충족해야 합니다.
|
||
-
UnaryPred
는
Predicate
요구사항을 충족해야 합니다.
|
||
반환값
두 번째 그룹의 첫 번째 요소에 대한 반복자.
복잡도
주어진 N 이 std:: distance ( first, last ) 인 경우:
ForwardIt
가
LegacyBidirectionalIterator
요구사항을 충족하는 경우, 그렇지 않으면 최대
N
회의 교환(swap).
예외
ExecutionPolicy
라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:
-
알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고
ExecutionPolicy가 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른ExecutionPolicy의 경우 동작은 구현에 따라 정의됩니다. - 알고리즘이 메모리 할당에 실패하는 경우, std::bad_alloc 이 throw됩니다.
가능한 구현
C++11 호환성을 유지하면서 오버로드를 구현합니다 (1) .
template<class ForwardIt, class UnaryPred> ForwardIt partition(ForwardIt first, ForwardIt last, UnaryPred p) { first = std::find_if_not(first, last, p); if (first == last) return first; for (auto i = std::next(first); i != last; ++i) if (p(*i)) { std::iter_swap(i, first); ++first; } return first; } |
예제
#include <algorithm> #include <forward_list> #include <iostream> #include <iterator> #include <vector> template<class ForwardIt> void quicksort(ForwardIt first, ForwardIt last) { if (first == last) return; auto pivot = *std::next(first, std::distance(first, last) / 2); auto middle1 = std::partition(first, last, [pivot](const auto& em) { return em < pivot; }); auto middle2 = std::partition(middle1, last, [pivot](const auto& em) { return !(pivot < em); }); quicksort(first, middle1); quicksort(middle2, last); } int main() { std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; std::cout << "Original vector: "; for (int elem : v) std::cout << elem << ' '; auto it = std::partition(v.begin(), v.end(), [](int i) {return i % 2 == 0;}); std::cout << "\nPartitioned vector: "; std::copy(std::begin(v), it, std::ostream_iterator<int>(std::cout, " ")); std::cout << "* "; std::copy(it, std::end(v), std::ostream_iterator<int>(std::cout, " ")); std::forward_list<int> fl {1, 30, -4, 3, 5, -4, 1, 6, -8, 2, -5, 64, 1, 92}; std::cout << "\nUnsorted list: "; for (int n : fl) std::cout << n << ' '; quicksort(std::begin(fl), std::end(fl)); std::cout << "\nSorted using quicksort: "; for (int fi : fl) std::cout << fi << ' '; std::cout << '\n'; }
가능한 출력:
Original vector: 0 1 2 3 4 5 6 7 8 9 Partitioned vector: 0 8 2 6 4 * 5 3 7 1 9 Unsorted list: 1 30 -4 3 5 -4 1 6 -8 2 -5 64 1 92 Sorted using quicksort: -8 -5 -4 -4 1 1 1 2 3 5 6 30 64 92
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 498 | C++98 |
std::partition
이
first
와
last 가 LegacyBidirectionalIterator 일 것을 요구함 |
LegacyForwardIterator
만
요구하도록 변경 |
| LWG 2150 | C++98 |
std::partition
이
p
를 만족하는 하나의 요소를
p 를 만족하지 않는 하나의 요소 앞에 배치하기만 하면 됨 |
요구 사항을
수정함 |
참고 항목
(C++11) |
범위가 주어진 조건자에 의해 분할되어 있는지 확인합니다 (함수 템플릿 & 알고리즘 함수 객체) |
(C++20) |
|
| 요소들을 두 그룹으로 나누되 각 그룹 내에서 상대적 순서를 유지합니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20) |
|
(C++20) |
요소 범위를 두 그룹으로 나눕니다 (알고리즘 함수 객체) |