Namespaces
Variants

std::replace_copy、std::replace_copy_if

来自 zh.cppreference.net
 
 
算法库
受约束算法和范围算法 (C++20)
受约束算法,例如 ranges::copy、ranges::sort...
排序及相关操作
划分操作
(C++11)    

排序操作
二分搜索操作
(在已划分的范围上)
集合操作(在已排序的范围上)
合并操作(在已排序的范围上)
堆操作
最小/最大操作
(C++11)
(C++17)
字典序比较操作
排列操作


 
定义于头文件 <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 起)
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 );
(3) (C++26 前)
(C++20 起 constexpr)
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 起)
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 );
(4) (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 执行。
这些重载仅当以下表达式的值为 true 时参与重载决议:

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>

(C++20 前)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>

(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 结尾的模板类型参数意味着需要满足相应迭代器类别的类型要求。

另见

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