partition_copy

partition_copy (분할 복사)

범위를 두 출력 범위로 나눠 복사하는 알고리즘이에요. 참인 원소들은 한 곳에, 거짓인 원소들은 다른 곳에 복사해요. <algorithm> 헤더에 있어요.

출처: cppreference

본문

partition_copy[first, last)에서 술어 p가 참인 원소를 d_first_true에, 거짓인 원소를 d_first_false에 복사해요. 원본은 그대로 두고요.

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 );   // (1)
  • 반환 값: {d_first_true 또는 d_first_false로 이동된 끝, ...} 쌍.
std::vector<int> v{1, 2, 3, 4, 5};
std::vector<int> evens, odds;
std::partition_copy(v.begin(), v.end(),
                    std::back_inserter(evens), std::back_inserter(odds),
                    [](int x){ return x % 2 == 0; });
// evens == {2,4}, odds == {1,3,5}

원본을 보존하면서 조건별로 두 컨테이너에 나눠 담고 싶을 때 써요. partition이 제자리 재배열이라면, partition_copy는 복사로 분리해요.

더 알아보기 (Learn more)

cppreference