ranges::unique_copy

ranges::unique_copy (중복 제거 복사 — ranges)

연속 중복을 제거한 결과를 출력 범위에 복사하는 ranges 버전 알고리즘이에요. 원본은 그대로 두고요. <algorithm> 헤더에 있어요.

출처: cppreference

본문

std::ranges::unique_copy[first, last)에서 연속 반복되는 원소 중 첫 번째만 남겨 result에 복사해요.

namespace std::ranges {
template< std::input_iterator I, std::sentinel_for<I> S,
          std::weakly_incrementable O, class Proj = std::identity,
          std::indirect_equivalence_relation<std::projected<I, Proj>> C = ranges::equal_to >
requires std::indirectly_copyable<I, O>
constexpr unique_copy_result<I, O>
    unique_copy( I first, S last, O result, C comp = {}, Proj proj = {} );
}
  • 반환 타입 unique_copy_result{in, out}.
std::vector<int> v{1, 1, 2, 3, 3, 3, 4};
std::vector<int> out;
std::ranges::unique_copy(v, std::back_inserter(out));
// out == {1,2,3,4}

unique가 제자리라면, unique_copy는 중복 제거 결과를 새 컨테이너에 담아 원본을 보존해요.

더 알아보기 (Learn more)

cppreference