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 的元素(使用 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) 使用 operator == value 进行恰好 N 次比较。
3,4) 恰好应用谓词 p N 次。

对于带有 ExecutionPolicy 的重载,如果 ForwardIt1 value_type 不满足 可移动构造 要求,则可能会产生性能开销。

异常

带有名为 ExecutionPolicy 模板参数的重载按以下方式报告错误:

  • 如果作为算法一部分调用的函数执行抛出异常,且 ExecutionPolicy 是某个 标准策略 ,则调用 std::terminate 。对于其他任何 ExecutionPolicy ,其行为由实现定义。
  • 如果算法无法分配内存,则抛出 std::bad_alloc

可能的实现

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;
}

注释

功能测试 标准 功能
__cpp_lib_algorithm_default_value_type 202403 (C++26) 列表初始化 用于算法 ( 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 << "处理前: " << std::quoted(str) << '\n';
    std::cout << "处理后:  \"";
    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
}

输出:

处理前: "#Return #Value #Optimization"
处理后:  "Return Value Optimization"
(2,2)(4,8)

缺陷报告

下列行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用范围 发布时行为 正确行为
LWG 779 C++98 要求 T 满足 EqualityComparable 要求,但
ForwardIt 的值类型并不总是 T
改为要求
* d_first = * first
表达式必须合法

参见

移除满足特定条件的元素
(函数模板)
复制元素范围到新位置
(函数模板)
复制范围并将元素划分为两组
(函数模板)
复制元素范围并忽略满足特定条件的元素
(算法函数对象)