命名空间
变体
操作

std::remove_copy, std::remove_copy_if

来自 cppreference.cn
< cpp‎ | 算法
 
 
算法库
有约束算法与针对范围的算法 (C++20)
有约束的算法,例如 ranges::copyranges::sort 等……
执行策略 (C++17)
排序及相关操作
划分操作
排序操作
二分搜索操作
(于已划分范围上)
集合操作(于已排序范围上)
归并操作(于已排序范围上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比较操作
排列操作
C 库
数值操作
未初始化内存上的操作
 
定义于头文件 <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 起)

将范围 [firstlast) 中的元素复制到从 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) 恰好进行 N 次与 value 的比较(使用 operator==)。
3,4) 恰好进行 N 次谓词 p 的应用。

对于带有 ExecutionPolicy 的重载,如果 ForwardIt1value_type 不可 MoveConstructible,可能会有性能开销。

[编辑] 异常

带有模板参数 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()
{
    // Erase the hash characters '#' on the fly.
    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";
 
    // Erase {1, 3} value on the fly.
    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 gets deduced
    #else
                     std::complex<double>{1, 3});
    #endif
}

输出

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

[编辑] 缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
LWG 779 C++98 要求 TEqualityComparable,但
ForwardIt 的值类型不总是 T
要求 *d_first = *first
是有效的,而不是

[编辑] 参阅

移除满足特定标准的元素
(函数模板) [编辑]
将一个范围的元素复制到一个新位置
(函数模板) [编辑]
复制一个范围,并将元素分成两组
(函数模板) [编辑]
复制一个范围的元素,忽略那些满足特定条件的元素
(算法函数对象)[编辑]