Namespaces
Variants

std:: unique_copy

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
헤더 파일에 정의됨 <algorithm>
template < class InputIt, class OutputIt >
OutputIt unique_copy ( InputIt first, InputIt last, OutputIt d_first ) ;
(1) (C++20부터 constexpr)
template < class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >

ForwardIt2 unique_copy ( ExecutionPolicy && policy, ForwardIt1 first,

ForwardIt1 last, ForwardIt2 d_first ) ;
(2) (C++17부터)
template < class InputIt, class OutputIt, class BinaryPred >

OutputIt unique_copy ( InputIt first, InputIt last,

OutputIt d_first, BinaryPred p ) ;
(3) (C++20부터 constexpr)
template < class ExecutionPolicy, class ForwardIt1,

class ForwardIt2, class BinaryPred >
ForwardIt2 unique_copy ( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 last,

ForwardIt2 d_first, BinaryPred p ) ;
(4) (C++17부터)

범위 [ first , last ) 의 요소들을 d_first 에서 시작하는 다른 범위로 복사합니다. 이때 연속된 동일 요소가 없도록 복사하며, 동일 요소 그룹에서 첫 번째 요소만 복사됩니다.

1) 요소들은 operator == 를 사용하여 비교됩니다.
만약 operator == 동치 관계 를 설정하지 않으면, 동작은 정의되지 않습니다.
3) 요소들은 주어진 이항 조건자 p 를 사용하여 비교됩니다.
만약 p 가 동치 관계를 설정하지 않으면, 동작은 정의되지 않습니다.
2,4) (1,3) 와 동일하지만, policy 에 따라 실행됩니다.
다음 모든 조건이 만족될 때만 이 오버로드들이 오버로드 해결에 참여합니다:

std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> true 인 경우.

(C++20 이전)

std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> true 인 경우.

(C++20 이후)

만약 * d_first = * first 가 유효하지 않으면 (C++20 이전) * first d_first 기록 가능 하지 않으면 (C++20 이후) 프로그램의 형식이 잘못되었습니다.

소스 범위와 대상 범위가 겹치는 경우 동작은 정의되지 않습니다.

InputIt 의 값 타입을 T 로 할 때, 오버로드 (1) 이나 (3) 가 다음 조건들 모두 를 만족시키지 않으면, 동작은 정의되지 않습니다:

(C++20 이전)
(C++20 이후)

목차

매개변수

first, last - 처리할 요소들의 소스 범위 를 정의하는 반복자 쌍
d_first - 대상 범위의 시작 지점
policy - 사용할 실행 정책
p - 요소들이 동등하게 처리되어야 할 경우 ​ true 를 반환하는 이항 predicate

predicate 함수의 시그니처는 다음에 해당해야 합니다:

bool pred ( const Type1 & a, const Type2 & b ) ;

시그니처에 const & 가 필요하지는 않지만, 함수는 전달된 객체를 수정해서는 안 되며, 값 범주 에 관계없이 (가능한 const) Type1 Type2 타입의 모든 값을 수용할 수 있어야 합니다 (따라서 Type1 & 는 허용되지 않으며 , Type1 Type1 에 대해 이동이 복사와 동등하지 않는 한 허용되지 않습니다 (C++11부터) ).
Type1 Type2 타입은 InputIt 타입의 객체가 역참조된 후 두 타입으로 암시적으로 변환될 수 있어야 합니다. ​

타입 요구사항
-
InputIt LegacyInputIterator 요구사항을 충족해야 합니다.
-
OutputIt LegacyOutputIterator 요구사항을 충족해야 합니다.
-
ForwardIt1, ForwardIt2 LegacyForwardIterator 요구사항을 충족해야 합니다.

반환값

마지막으로 기록된 요소의 다음 위치를 가리키는 출력 반복자.

복잡도

주어진 N std:: distance ( first, last ) 인 경우:

1,2) 정확히 max(0,N-1) 번의 비교를 operator == 를 사용하여 수행합니다.
3,4) 정확히 max(0,N-1) 번의 술어 p 적용.

오버로드 (2,4) 의 경우, ForwardIt1 의 값 타입이 CopyConstructible CopyAssignable 모두를 만족하지 않을 경우 성능 비용이 발생할 수 있습니다.

예외

ExecutionPolicy 라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:

  • 알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고 ExecutionPolicy 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른 ExecutionPolicy 의 경우 동작은 구현에 따라 정의됩니다.
  • 알고리즘이 메모리 할당에 실패하는 경우, std::bad_alloc 이 throw됩니다.

가능한 구현

구현체는 또한 libstdc++ libc++ 에서 확인할 수 있습니다.

참고 사항

만약 InputIt LegacyForwardIterator 를 만족한다면, 이 함수는 중복을 감지하기 위해 입력을 재읽습니다.

그렇지 않고, OutputIt LegacyForwardIterator 를 만족하며, InputIt 의 값 타입이 OutputIt 의 값 타입과 동일한 경우, 이 함수는 * d_first * first 를 비교합니다.

그렇지 않으면, 이 함수는 * first 를 로컬 요소 복사본과 비교합니다.

예제

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
int main()
{
    std::string s1 {"A string with mmmany letters!"};
    std::cout << "Before: " << s1 << '\n';
    std::string s2;
    std::unique_copy(s1.begin(), s1.end(), std::back_inserter(s2),
                     [](char c1, char c2) { return c1 == 'm' && 'm' == c2; });
    std::cout << "After:  " << s2 << '\n';
}

출력:

Before: A string with mmmany letters!
After:  A string with many letters!

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 239 C++98 predicate가 std:: distance ( first, last ) 번 적용됨 한 번 덜 적용됨
(비어 있지 않은 범위의 경우)
LWG 241 C++98 InputIt 의 value type이 CopyConstructible 일 필요가 없었음 조건부로 요구됨
LWG 538 C++98 InputIt 의 value type이 CopyAssignable 일 필요가 없었음 조건부로 요구됨
LWG 2439 C++98 InputIt 의 value type이
CopyConstructible 일 필요가 없었음 ( OutputIt LegacyForwardIterator 인 경우에도)
조건부로 요구됨

참고 항목

동일한(또는 주어진 조건자를 만족하는) 첫 두 개의 인접 항목을 찾습니다
(function template)
범위에서 연속적인 중복 요소를 제거합니다
(function template)
요소들의 범위를 새로운 위치로 복사합니다
(function template)
연속적인 중복이 없는 일부 범위 요소들의 사본을 생성합니다
(algorithm function object)