ranges::partition_copy

ranges::partition_copy (분할 복사 — ranges)

범위를 조건별로 두 출력으로 나눠 복사하는 ranges 버전 알고리즘이에요. <algorithm> 헤더에 있어요.

출처: cppreference

본문

std::ranges::partition_copy는 술어가 참인 원소를 out_true에, 거짓인 원소를 out_false에 복사해요. 원본은 그대로 둬요.

namespace std::ranges {
template< std::input_iterator I, std::sentinel_for<I> S,
          std::weakly_incrementable O1, std::weakly_incrementable O2,
          class Proj = std::identity,
          std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
requires std::indirectly_copyable<I, O1> && std::indirectly_copyable<I, O2>
constexpr partition_copy_result<I, O1, O2>
    partition_copy( I first, S last, O1 out_true, O2 out_false,
                    Pred pred, Proj proj = {} );
}
  • 반환 타입 partition_copy_result{in, out_true, out_false}.
std::vector<int> v{1, 2, 3, 4, 5};
std::vector<int> evens, odds;
std::ranges::partition_copy(v, std::back_inserter(evens),
                            std::back_inserter(odds),
                            [](int x){ return x % 2 == 0; });
// evens == {2,4}, odds == {1,3,5}

원본을 보존하며 조건별로 나눠 담는 ranges 버전이에요.

더 알아보기 (Learn more)

cppreference