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 起)

从源范围 [ first , last ) 复制元素到以 result 开始的目标范围,跳过(经 proj 投影后)满足特定条件的元素。若源范围与目标范围重叠则行为未定义。

1) 忽略所有等于 value 的元素。
3) 忽略所有谓词 pred 返回 true 的元素。
2,4) (1,3) 相同,但使用 r 作为源范围,如同以 ranges:: begin ( r ) 作为 first ,并以 ranges:: end ( r ) 作为 last

本页面描述的函数式实体是 算法函数对象 (非正式称为 niebloids ),即:

目录

参数

first, last - 定义待处理元素源 范围 的迭代器-哨位对
r - 元素的源范围
result - 目标范围的起始位置
value - 被复制元素的值
comp - 用于比较投影元素的二元谓词
proj - 应用于元素的投影

返回值

{ last, result + N } ,其中 N 为被复制的元素数量。

复杂度

恰好应用对应的谓词 comp 和任何投影 proj ranges:: distance ( first, last ) 次。

注释

该算法是稳定的,即保持被复制元素的相对顺序。

功能测试 标准 功能
__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<<a

示例

#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 << "处理前: " << std::quoted(str) << '\n';
    std::cout << "处理后:  \"";
    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);
    println("目标数组:", 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);
}

输出:

处理前: "#Small #Buffer #Optimization"
处理后:  "Small Buffer Optimization"
源数组: (1,0) (0,1) (2,-1) (3,2) (4,-3)
目标数组: (0,1) (3,2)
nums:   (2,2) (1,3) (4,8) (1,3)
outs:   (2,2) (4,8)

参见

移除满足特定条件的元素
(算法函数对象)
将元素范围复制到新位置
(算法函数对象)
将指定数量的元素复制到新位置
(算法函数对象)
以逆序复制元素范围
(算法函数对象)
复制范围,并将满足特定条件的元素替换为其他值
(算法函数对象)
创建反转后的范围副本
(算法函数对象)
复制并旋转元素范围
(算法函数对象)
创建不包含连续重复元素的范围副本
(算法函数对象)
复制元素范围,忽略满足特定条件的元素
(函数模板)