Namespaces
Variants

std::ranges:: remove_copy, std::ranges:: remove_copy_if, std::ranges:: remove_copy_result, std::ranges:: remove_copy_if_result

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
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
헤더 파일에 정의됨 <algorithm>
호출 시그니처
(1)
template < std:: input_iterator I, std:: sentinel_for < I > S,

std:: weakly_incrementable O, class T, class Proj = std:: identity >
requires std:: indirectly_copyable < I, O > &&
std:: indirect_binary_predicate
< ranges:: equal_to , std :: projected < I, Proj > , const T * >
constexpr remove_copy_result < I, O >

remove_copy ( I first, S last, O result, const T & value, Proj proj = { } ) ;
(C++20부터)
(C++26까지)
template < std:: input_iterator I, std:: sentinel_for < I > S,

std:: weakly_incrementable O, class Proj = std:: identity ,
class T = std :: projected_value_t < I, Proj > >
requires std:: indirectly_copyable < I, O > &&
std:: indirect_binary_predicate
< ranges:: equal_to , std :: projected < I, Proj > , const T * >
constexpr remove_copy_result < I, O >

remove_copy ( I first, S last, O result, const T & value, Proj proj = { } ) ;
(C++26부터)
(2)
template < ranges:: input_range R,

std:: weakly_incrementable O, class T, class Proj = std:: identity >
requires std:: indirectly_copyable < ranges:: iterator_t < R > , O > &&
std:: indirect_binary_predicate
< ranges:: equal_to ,
std :: projected < ranges:: iterator_t < R > , Proj > , const T * >
constexpr remove_copy_result < ranges:: borrowed_iterator_t < R > , O >

remove_copy ( R && r, O result, const T & value, Proj proj = { } ) ;
(C++20부터)
(C++26까지)
template < ranges:: input_range R,

std:: weakly_incrementable O, class Proj = std:: identity ,
class T = std :: projected_value_t < ranges:: iterator_t < R > , Proj > >
requires std:: indirectly_copyable < ranges:: iterator_t < R > , O > &&
std:: indirect_binary_predicate
< ranges:: equal_to ,
std :: projected < ranges:: iterator_t < R > , Proj > , const T * >
constexpr remove_copy_result < ranges:: borrowed_iterator_t < R > , O >

remove_copy ( R && r, O result, const T & value, Proj proj = { } ) ;
(C++26부터)
template < std:: input_iterator I, std:: sentinel_for < I > S,

std:: weakly_incrementable O, class Proj = std:: identity ,
std:: indirect_unary_predicate < std :: projected < I, Proj >> Pred >
requires std:: indirectly_copyable < I, O >
constexpr remove_copy_if_result < I, O >

remove_copy_if ( I first, S last, O result, Pred pred, Proj proj = { } ) ;
(3) (C++20 이후)
template < ranges:: input_range R,

std:: weakly_incrementable O, class Proj = std:: identity ,
std:: indirect_unary_predicate <
std :: projected < ranges:: iterator_t < R > , Proj >> Pred >
requires std:: indirectly_copyable < ranges:: iterator_t < R > , O >
constexpr remove_copy_if_result < ranges:: borrowed_iterator_t < R > , O >

remove_copy_if ( R && r, O result, Pred pred, Proj proj = { } ) ;
(4) (C++20 이후)
도우미 타입
template < class I, class O >
using remove_copy_result = ranges:: in_out_result < I, O > ;
(5) (C++20 이후)
template < class I, class O >
using remove_copy_if_result = ranges:: in_out_result < I, O > ;
(6) (C++20 이후)

소스 범위 [ first , last ) 에서 요소들을 대상 범위의 result 에서 시작하는 위치로 복사하되, ( proj 로 투영된 후) 특정 조건을 만족하는 요소들은 생략합니다. 소스와 대상 범위가 겹치는 경우 동작은 정의되지 않습니다.

1) value 와 동일한 모든 요소들을 무시합니다.
3) 술어 pred true 를 반환하는 모든 요소들을 무시합니다.
2,4) (1,3) 와 동일하지만, r 를 소스 범위로 사용합니다. 마치 ranges:: begin ( r ) first 로, 그리고 ranges:: end ( r ) last 로 사용하는 것과 같습니다.

이 페이지에서 설명하는 함수형 개체들은 algorithm function objects (일반적으로 niebloids 로 알려진)입니다. 즉:

목차

매개변수

first, last - 처리할 요소들의 소스 범위 를 정의하는 반복자-감시자 쌍
r - 요소들의 소스 범위
result - 대상 범위의 시작점
value - 복사 하지 않을 요소들의 값
comp - 투영된 요소들을 비교하기 위한 이항 조건자
proj - 요소들에 적용할 투영

반환값

{ last, result + N } , 여기서 N 은 복사된 원소의 개수입니다.

복잡도

정확히 ranges:: distance ( first, last ) 번의 해당 predicate comp 및 projection proj 적용이 수행됩니다.

참고 사항

알고리즘은 안정적입니다. 즉, 복사된 요소들의 상대적 순서를 유지합니다.

기능 테스트 매크로 표준 기능
__cpp_lib_algorithm_default_value_type 202403 (C++26) 알고리즘을 위한 목록 초기화 ( 1,2 )

가능한 구현

remove_copy (1,2)
struct remove_copy_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S,
             std::weakly_incrementable O, class Proj = std::identity,
             class T = std::projected_value_t<I, Proj>>
    requires std::indirectly_copyable<I, O> &&
             std::indirect_binary_predicate<ranges::equal_to,
                                            std::projected<I, Proj>, const T*>
    constexpr ranges::remove_copy_result<I, O>
        operator()(I first, S last, O result, const T& value, Proj proj = {}) const
    {
        for (; !(first == last); ++first)
            if (value != std::invoke(proj, *first))
            {
                *result = *first;
                ++result;
            }
        return {std::move(first), std::move(result)};
    }
    template<ranges::input_range R, 
             std::weakly_incrementable O, class Proj = std::identity,
             class T = std::projected_value_t<ranges::iterator_t<R>, Proj>>
    requires std::indirectly_copyable<ranges::iterator_t<R>, O> &&
             std::indirect_binary_predicate<ranges::equal_to,
             std::projected<ranges::iterator_t<R>, Proj>, const T*>
    constexpr ranges::remove_copy_result<ranges::borrowed_iterator_t<R>, O>
        operator()(R&& r, O result, const T& value, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(result), value,
                       std::move(proj));
    }
};
inline constexpr remove_copy_fn remove_copy {};
remove_copy_if (3,4)
struct remove_copy_if_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O,
             class Proj = std::identity,
             std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
    requires std::indirectly_copyable<I, O>
    constexpr ranges::remove_copy_if_result<I, O>
        operator()(I first, S last, O result, Pred pred, Proj proj = {}) const
    {
        for (; first != last; ++first)
            if (false == std::invoke(pred, std::invoke(proj, *first)))
            {
                *result = *first;
                ++result;
            }
        return {std::move(first), std::move(result)};
    }
    template<ranges::input_range R, std::weakly_incrementable O,
             class Proj = std::identity,
             std::indirect_unary_predicate<
                 std::projected<ranges::iterator_t<R>, Proj>> Pred>
    requires std::indirectly_copyable<ranges::iterator_t<R>, O>
    constexpr ranges::remove_copy_if_result<ranges::borrowed_iterator_t<R>, O>
        operator()(R&& r, O result, Pred pred, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(result),
                       std::move(pred), std::move(proj));
    }
};
inline constexpr remove_copy_if_fn remove_copy_if {};

예제

#include <algorithm>
#include <array>
#include <complex>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <string_view>
#include <vector>
void println(const auto rem, const auto& v)
{
    std::cout << rem << ' ';
    for (const auto& e : v)
        std::cout << e << ' ';
    std::cout << '\n';
}
int main()
{
    // 주어진 문자열에서 해시 기호를 필터링합니다.
    const std::string_view str{"#Small #Buffer #Optimization"};
    std::cout << "before: " << std::quoted(str) << '\n';
    std::cout << "after:  \"";
    std::ranges::remove_copy(str.begin(), str.end(),
                             std::ostream_iterator<char>(std::cout), '#');
    std::cout << "\"\n";
    // 허수부가 양수인 복소수만 복사합니다.
    using Ci = std::complex<int>;
    constexpr std::array<Ci, 5> source
    {
        Ci{1, 0}, Ci{0, 1}, Ci{2, -1}, Ci{3, 2}, Ci{4, -3}
    };
    std::vector<std::complex<int>> target;
    std::ranges::remove_copy_if
    (
        source,
        std::back_inserter(target),
        [](int imag) { return imag <= 0; },
        [](Ci z) { return z.imag(); }
    );
    println("source:", source);
    println("target:", target);
    std::vector<std::complex<float>> nums{{2, 2}, {1, 3}, {4, 8}, {1, 3}};
    std::vector<std::complex<double>> outs;
    #ifdef __cpp_lib_algorithm_default_value_type
        std::remove_copy(nums.cbegin(), nums.cend(), std::back_inserter(outs),
                         {1, 3}); // T는 std::complex<float>로 추론됩니다
    #else
        std::remove_copy(nums.cbegin(), nums.cend(), std::back_inserter(outs),
                         std::complex<float>{1, 3});
    #endif
    println("nums:  ", nums);
    println("outs:  ", outs);
}

출력:

before: "#Small #Buffer #Optimization"
after:  "Small Buffer Optimization"
source: (1,0) (0,1) (2,-1) (3,2) (4,-3)
target: (0,1) (3,2)
nums:   (2,2) (1,3) (4,8) (1,3)
outs:   (2,2) (4,8)

참고 항목

특정 조건을 만족하는 요소들을 제거함
(알고리즘 함수 객체)
요소들의 범위를 새로운 위치로 복사함
(알고리즘 함수 객체)
지정된 개수의 요소들을 새로운 위치로 복사함
(알고리즘 함수 객체)
요소들의 범위를 역순으로 복사함
(알고리즘 함수 객체)
범위를 복사하면서 특정 조건을 만족하는 요소들을 다른 값으로 대체함
(알고리즘 함수 객체)
역순으로 된 범위의 복사본을 생성함
(알고리즘 함수 객체)
요소들의 범위를 복사하고 회전시킴
(알고리즘 함수 객체)
연속된 중복 요소가 없는 일부 범위의 복사본을 생성함
(알고리즘 함수 객체)
특정 조건을 만족하는 요소들을 생략하고 범위를 복사함
(함수 템플릿)