命名空间
变体
操作

std::replace_copy, std::replace_copy_if

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

将范围 [firstlast) 中的元素复制到从 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 起)

如果表达式 *firstnew_value 的任何结果不可写入d_first,则程序是非良构的。

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

目录

[编辑] 参数

first, last - 定义要复制的元素的源 范围 的迭代器对
d_first - 目标范围的开头
old_value - 要替换的元素的值
policy - 要使用的 执行策略
p - 一元谓词,如果元素值应该被替换,则返回 true

对于类型(可能是 const)VT 的每个参数 v,表达式 p(v) 必须可转换为 bool,其中 VTInputIt 的值类型,无论值类别如何,并且不得修改 v。因此,不允许使用 VT& 参数类型,也不允许使用 VT,除非对于 VT 移动等同于复制(C++11 起)。​

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

[编辑] 返回值

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

[编辑] 复杂度

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

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

[编辑] 异常

带有模板参数 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}); // Possible, since the T is deduced.
    #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 满足
LegacyIterator 的要求[1]
更正为
LegacyInputIterator(传统输入迭代器)
  1. C++ 标准中的实际缺陷是模板参数 InputIterator 被错误指定为 Iterator。这会影响类型要求,因为 C++ 标准规定对于算法库中的函数模板,其名称以 Iterator 结尾的模板类型参数暗示了相应迭代器类别的类型要求。

[编辑] 另请参阅

用另一个值替换所有满足特定条件的值
(函数模板) [编辑]
移除满足特定标准的元素
(函数模板) [编辑]
复制一个范围,同时用另一个值替换满足特定条件的元素
(算法函数对象)[编辑]