Namespaces
Variants

std:: replace_copy, std:: replace_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
replace_copy replace_copy_if

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
定义于头文件 <algorithm>
template < class InputIt, class OutputIt, class T >

OutputIt replace_copy ( InputIt first, InputIt last, OutputIt d_first,

const T & old_value, const T & new_value ) ;
(1) (自 C++20 起为 constexpr)
template < class ExecutionPolicy,

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

const T & old_value, const T & new_value ) ;
(2) (自 C++17 起)
(3)
template < class InputIt, class OutputIt, class UnaryPred, class T >

OutputIt replace_copy_if
( InputIt first, InputIt last, OutputIt d_first,

UnaryPred p, const T & new_value ) ;
(自 C++20 起为 constexpr)
(C++26 前)
template < class InputIt, class OutputIt, class UnaryPred,

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

UnaryPred p, const T & new_value ) ;
(自 C++26 起)
(4)
template < class ExecutionPolicy, class ForwardIt1, class ForwardIt2,

class UnaryPred, class T >
ForwardIt2 replace_copy_if
( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first,

UnaryPred p, const T & new_value ) ;
(自 C++17 起)
(C++26 前)
template < class ExecutionPolicy, class ForwardIt1, class ForwardIt2,

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

UnaryPred p, const T & new_value ) ;
(自 C++26 起)

将范围 [ first , last ) 中的元素复制到始于 d_first 的另一范围,同时将所有满足特定条件的元素替换为 new_value

1) 将所有等于 old_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 起)

如果表达式 * first new_value 的任一结果无法写入 d_first ,则程序非良构。

如果源范围和目标范围重叠,则行为未定义。

目录

参数

first, last - 定义待复制元素源 范围 的迭代器对
d_first - 目标范围的起始位置
old_value - 待替换的元素值
policy - 使用的 执行策略
p - 一元谓词,当元素值应被替换时返回​ true

表达式 p ( v ) 必须可转换为 bool ,其中参数 v 为类型(可能为 const) VT VT InputIt 的值类型),且不依赖于 值类别 ,同时不得修改 v 。因此,不允许使用 VT & 作为参数类型 ,也不允许使用 VT ,除非对于 VT 类型移动操作等同于复制操作 (C++11 起) 。 ​

new_value - 用作替换的值
类型要求
-
InputIt 必须满足 LegacyInputIterator 的要求。
-
OutputIt 必须满足 LegacyOutputIterator 的要求。
-
ForwardIt1, ForwardIt2 必须满足 LegacyForwardIterator 的要求。

返回值

指向最后一个被复制元素之后位置的迭代器。

复杂度

给定 N std:: distance ( first, last )

1,2) 使用 operator == 进行恰好 N 次比较。
3,4) 恰好应用谓词 p N 次。

异常

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

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

可能的实现

replace_copy (1)
template<class InputIt, class OutputIt, class T>
OutputIt replace_copy(InputIt first, InputIt last, OutputIt d_first,
                      const T& old_value, const T& new_value)
{
    for (; first != last; ++first)
        *d_first++ = (*first == old_value) ? new_value : *first;
    return d_first;
}
replace_copy_if (3)
template<class InputIt, class OutputIt, class UnaryPred,
         class T = typename std::iterator_traits<ForwardIt>::value_type>
OutputIt replace_copy_if(InputIt first, InputIt last, OutputIt d_first,
                         UnaryPred p, const T& new_value)
{
    for (; first != last; ++first)
        *d_first++ = p(*first) ? new_value : *first;
    return d_first;
}

注释

功能测试 标准 功能
__cpp_lib_algorithm_default_value_type 202403 (C++26) 列表初始化 用于算法 ( 3,4 )

示例

#include <algorithm>
#include <complex>
#include <iostream>
#include <vector>
void println(const auto& seq)
{
    for (const auto& e : seq)
        std::cout << e << ' ';
    std::cout << '\n';
}
int main()
{
    std::vector<short> src{3, 1, 4, 1, 5, 9, 2, 6, 5};
    println(src);
    std::vector<int> dst(src.size());
    std::replace_copy_if(src.cbegin(), src.cend(),
                         dst.begin(),
                         [](short n){ return n > 5; }, 0);
    println(dst);
    std::vector<std::complex<double>> src2{{1, 3}, {2, 4}, {3, 5}},
                                      dst2(src2.size());
    println(src2);
    #ifdef __cpp_lib_algorithm_default_value_type
        std::replace_copy_if(src2.cbegin(), src2.cend(), dst2.begin(),
            [](std::complex<double> z){ return std::abs(z) < 5; },
            {4, 2}); // 可行,因为 T 被推导得出。
    #else
        std::replace_copy_if(src2.cbegin(), src2.cend(), dst2.begin(),
            [](std::complex<double> z){ return std::abs(z) < 5; },
            std::complex<double>{4, 2});
    #endif
    println(dst2);
}

输出:

3 1 4 1 5 9 2 6 5 
3 1 4 1 5 0 2 0 5 
(1,3) (2,4) (3,5) 
(4,2) (4,2) (3,5)

缺陷报告

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

缺陷报告 适用版本 发布时行为 正确行为
LWG 283 C++98 要求 T 必须是 可复制赋值 (且 replace_copy 还需满足
可相等比较 ),但 InputIt 的值类型并不总是 T
移除该要求
LWG 337 C++98 replace_copy_if 仅要求 InputIt
满足 遗留迭代器 [1] 的要求
修正为
遗留输入迭代器
  1. C++标准中的实际缺陷在于模板参数 InputIterator 被错误指定为 Iterator 。这会影响类型要求,因为C++标准规定:对于算法库中的函数模板,名称以 Iterator 结尾的模板类型参数意味着需要满足相应迭代器类别的类型要求。

参见

将满足特定条件的所有值替换为另一个值
(函数模板)
移除满足特定条件的元素
(函数模板)
复制范围,将满足特定条件的元素替换为另一个值
(算法函数对象)