unique_copy
unique_copy (중복 제거 복사)
연속 중복을 제거한 결과를 출력 범위에 복사하는 알고리즘이에요. 원본은 그대로 두고요. <algorithm> 헤더에 있어요.
출처: cppreference
본문
unique_copy는 [first, last)에서 연속 반복되는 원소 중 첫 번째만 남겨 d_first에 복사해요.
template< class InputIt, class OutputIt >
OutputIt unique_copy( InputIt first, InputIt last, OutputIt d_first ); // (1)
비교기 버전도 있어요.
template< class InputIt, class OutputIt, class BinaryPred >
OutputIt unique_copy( InputIt first, InputIt last, OutputIt d_first,
BinaryPred p ); // (2)
- 반환 값: 마지막으로 복사한 원소 다음 반복자.
std::vector<int> v{1, 1, 2, 3, 3, 3, 4};
std::vector<int> out;
std::unique_copy(v.begin(), v.end(), std::back_inserter(out));
// out == {1,2,3,4}
unique가 제자리라면, unique_copy는 중복 제거 결과를 새 컨테이너에 담아 원본을 보존해요.