algorithm_partition_copy
algorithm_partition_copy (파티셔닝하며 복사)
std::partition_copy는 소스 범위를 술어 p에 따라 두 목적지 범위로 복사해서 나눠요. 조건을 만족하는 요소와 아닌 요소를 서로 다른 범위로 보내요.
출처: cppreference
본문
std::partition_copy는 소스 범위 [first, last)의 요소들 중 p를 만족하는 요소를 d_first_true로, 만족하지 않는 요소를 d_first_false로 복사해요. <algorithm> 헤더에 정의되어 있어요.
template< class InputIt, class OutputIt1,
class OutputIt2, class UnaryPred >
std::pair<OutputIt1, OutputIt2>
partition_copy( InputIt first, InputIt last,
OutputIt1 d_first_true, OutputIt2 d_first_false,
UnaryPred p );
병렬 실행 정책을 받는 오버로드도 있어요.
반환값 (Return value)
(d_first_true에 복사된 마지막 요소 다음의 반복자, d_first_false에 복사된 마지막 요소 다음의 반복자)` 쌍이에요.
복잡도 (Complexity)
N을 std::distance(first, last)라고 하면 정확히 N번의 p 적용이 필요해요.
예제 (Example)
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
std::vector<int> src{1, 2, 3, 4, 5, 6, 7, 8};
std::vector<int> even, odd;
std::partition_copy(src.begin(), src.end(),
std::back_inserter(even), std::back_inserter(odd),
[](int x) { return x % 2 == 0; });
for (int x : even) std::cout << x << ' ';
std::cout << "| ";
for (int x : odd) std::cout << x << ' ';
std::cout << '\n';
}
출력:
2 4 6 8 | 1 3 5 7