Namespaces
Variants

std:: remove_copy, std:: remove_copy_if

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
remove_copy remove_copy_if
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>
(1)
template < class InputIt, class OutputIt, class T >

OutputIt remove_copy ( InputIt first, InputIt last,

OutputIt d_first, const T & value ) ;
(C++20부터 constexpr)
(C++26까지)
template < class InputIt, class OutputIt,

class T = typename std:: iterator_traits
< InputIt > :: value_type >
constexpr OutputIt remove_copy ( InputIt first, InputIt last,

OutputIt d_first, const T & value ) ;
(C++26부터)
(2)
template < class ExecutionPolicy,

class ForwardIt1, class ForwardIt2, class T >
ForwardIt2 remove_copy ( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 last,

ForwardIt2 d_first, const T & value ) ;
(C++17부터)
(C++26까지)
template < class ExecutionPolicy,

class ForwardIt1, class ForwardIt2,
class T = typename std:: iterator_traits
< ForwardIt1 > :: value_type >
ForwardIt2 remove_copy ( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 last,

ForwardIt2 d_first, const T & value ) ;
(C++26부터)
template < class InputIt, class OutputIt, class UnaryPred >

OutputIt remove_copy_if ( InputIt first, InputIt last,

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

class ForwardIt1, class ForwardIt2, class UnaryPred >
ForwardIt2 remove_copy_if ( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 last,

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

범위 [ first , last ) 에서 특정 조건을 만족하는 요소들을 생략하고, d_first 로 시작하는 다른 범위로 요소들을 복사합니다.

1) value 와 동일한 모든 요소들을 무시합니다 (using operator == ).
3) 술어 p true 를 반환하는 모든 요소들을 무시합니다.
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 이후) 프로그램의 형식이 잘못되었습니다.

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

목차

매개변수

first, last - 복사할 요소들의 소스 범위 를 정의하는 반복자 쌍
d_first - 대상 범위의 시작 지점
value - 복사하지 않을 요소의 값
policy - 사용할 실행 정책
타입 요구사항
-
InputIt LegacyInputIterator 요구사항을 충족해야 함
-
OutputIt LegacyOutputIterator 요구사항을 충족해야 함
-
ForwardIt1, ForwardIt2 LegacyForwardIterator 요구사항을 충족해야 함
-
UnaryPred Predicate 요구사항을 충족해야 함

반환값

복사된 마지막 요소의 다음 요소를 가리키는 반복자.

복잡도

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

1,2) 정확히 N 번의 비교를 value 와 수행하며, operator == 를 사용합니다.
3,4) 정확히 N 번의 predicate p 적용.

ExecutionPolicy를 사용하는 오버로드의 경우, ForwardIt1 value_type MoveConstructible 가 아닐 경우 성능 저하가 발생할 수 있습니다.

예외

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

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

가능한 구현

remove_copy (1)
template<class InputIt, class OutputIt,
         class T = typename std::iterator_traits<InputIt>::value_type>
constexpr OutputIt remove_copy(InputIt first, InputIt last,
                               OutputIt d_first, const T& value)
{
    for (; first != last; ++first)
        if (!(*first == value))
            *d_first++ = *first;
    return d_first;
}
remove_copy_if (3)
template<class InputIt, class OutputIt, class UnaryPred>
constexpr OutputIt remove_copy_if(InputIt first, InputIt last,
                                  OutputIt d_first, UnaryPred p)
{
    for (; first != last; ++first)
        if (!p(*first))
            *d_first++ = *first;
    return d_first;
}
**참고**: HTML 태그, 속성, ,
,  태그 내부의 내용, 그리고 C++ 관련 용어들은 번역하지 않고 원본 그대로 유지했습니다.

참고 사항

기능 테스트 매크로 표준 기능
__cpp_lib_algorithm_default_value_type 202403 (C++26) 목록 초기화 for algorithms ( 1,2 )

예제

#include <algorithm>
#include <complex>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
int main()
{
    // 해시 문자 '#'을 실시간으로 제거합니다.
    std::string str = "#Return #Value #Optimization";
    std::cout << "before: " << std::quoted(str) << '\n';
    std::cout << "after:  \"";
    std::remove_copy(str.begin(), str.end(),
                     std::ostream_iterator<char>(std::cout), '#');
    std::cout << "\"\n";
    // {1, 3} 값을 실시간으로 제거합니다.
    std::vector<std::complex<double>> nums{{2, 2}, {1, 3}, {4, 8}, {1, 3}};
    std::remove_copy(nums.begin(), nums.end(),
                     std::ostream_iterator<std::complex<double>>(std::cout),
    #ifdef __cpp_lib_algorithm_default_value_type
                     {1, 3}); // T가 추론됨
    #else
                     std::complex<double>{1, 3});
    #endif
}

출력:

before: "#Return #Value #Optimization"
after:  "Return Value Optimization"
(2,2)(4,8)

결함 보고서

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

DR 적용 대상 게시된 동작 올바른 동작
LWG 779 C++98 T EqualityComparable 요구사항을 만족해야 했으나,
ForwardIt 의 값 타입이 항상 T 는 아님
대신 * d_first = * first
가 유효하도록 요구

참고 항목

특정 조건을 만족하는 요소들을 제거함
(함수 템플릿)
요소들의 범위를 새로운 위치로 복사함
(함수 템플릿)
요소들을 두 그룹으로 나누어 범위를 복사함
(함수 템플릿)
특정 조건을 만족하는 요소들을 제외하고 범위를 복사함
(알고리즘 함수 객체)