std::replace_copy, std::replace_copy_if
|   在标头  <algorithm> 定义
  | 
||
| (1) | ||
|   template< class InputIt, class OutputIt, class T > OutputIt replace_copy( InputIt first, InputIt last, OutputIt d_first,  | 
(C++20 前) | |
|   template< class InputIt, class OutputIt, class T > constexpr OutputIt replace_copy( InputIt first, InputIt last, OutputIt d_first,  | 
(C++20 起) | |
|   template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class T > ForwardIt2 replace_copy( ExecutionPolicy&& policy,  | 
(2) | (C++17 起) | 
| (3) | ||
|   template< class InputIt, class OutputIt, class UnaryPredicate, class T > OutputIt replace_copy_if( InputIt first, InputIt last, OutputIt d_first,  | 
(C++20 前) | |
|   template< class InputIt, class OutputIt, class UnaryPredicate, class T > constexpr OutputIt replace_copy_if( InputIt first, InputIt last,  | 
(C++20 起) | |
|   template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,           class UnaryPredicate, class T >  | 
(4) | (C++17 起) | 
复制来自范围 [first, last) 的元素到始于 d_first 的范围,复制过程中以 new_value 替换所有满足特定判别标准的元素。源范围与目标范围重叠时行为未定义。
operator== 比较)old_value 的元素。目录 | 
[编辑] 参数
| first, last | - | 要复制的元素范围 | 
| d_first | - | 目标范围的起始 | 
| old_value | - | 要被替换的元素值 | 
| policy | - | 所用的执行策略。细节见执行策略。 | 
| p | - |  如果应该替换元素则返回 true 的一元谓词。 对每个(可为 const 的)   | 
| new_value | - | 用作替换者的元素值 | 
| 类型要求 | ||
 -InputIt 必须符合老式输入迭代器 (LegacyInputIterator)  的要求。
 | ||
 -OutputIt 必须符合老式输出迭代器 (LegacyOutputIterator)  的要求。
 | ||
 -ForwardIt1, ForwardIt2 必须符合老式向前迭代器 (LegacyForwardIterator)  的要求。
 | ||
| -表达式 *first 和 new_value 的结果都必须可写入 d_first。 | ||
[编辑] 返回值
指向最后复制元素后一元素的迭代器。
[编辑] 复杂度
给定 N 为 std::distance(first, last):
N 次 operator== 与 old_value 进行比较N 次谓词 p[编辑] 异常
拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:
-  如果作为算法一部分调用的函数的执行抛出异常,且 
ExecutionPolicy是标准策略之一,那么调用 std::terminate。对于任何其他ExecutionPolicy,行为由实现定义。 - 如果算法无法分配内存,那么抛出 std::bad_alloc。
 
[编辑] 可能的实现
| replace_copy | 
|---|
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 | 
template<class InputIt, class OutputIt, class UnaryPredicate, class T> OutputIt replace_copy_if(InputIt first, InputIt last, OutputIt d_first, UnaryPredicate p, const T& new_value) { for (; first != last; ++first) *d_first++ = p(*first) ? new_value : *first; return d_first; }  | 
[编辑] 示例
下列代码打印 vector,同时临时将所有超过 5 的值替换为 99。
#include <algorithm> #include <vector> #include <iostream> #include <iterator> int main() { std::vector<int> v{5, 7, 4, 2, 8, 6, 1, 9, 0, 3}; std::replace_copy_if(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "), [](int n){ return n > 5; }, 99); std::cout << '\n'; }
输出:
5 99 4 2 99 99 1 99 0 3
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 | 
|---|---|---|---|
| LWG 283 | C++98 |  T 需要是 可复制赋值 (CopyAssignable)  的(对于 replace_copy 还需要是可相等比较 (EqualityComparable) 的),但是 InputIt 的值类型不一定是 T
 | 
移除该要求 | 
| LWG 337 | C++98 |  对于 replace_copy_if,InputIt 只需要满足 老式迭代器 (LegacyIterator) 的要求[1]  | 
 改成 老式输入迭代器 (LegacyInputIterator)  | 
- ↑ 实际上 C++ 标准中的缺陷是模板类型形参 
InputIterator被误标为Iterator。由于 C++ 标准规定算法库函数模板的名字以Iterator的模板形参的类型要求需要与对应的迭代器类别的类型要求一致,迭代器模板类型形参的误标会影响到该模板形参的类型要求。 
[编辑] 参阅
|    将所有满足特定判别标准的值替换为另一个值   (函数模板)  | |
|   移除满足特定判别标准的元素   (函数模板)  | |
|    (C++20)(C++20)  | 
  复制一个范围内的元素,并将满足特定判别标准的元素替换为另一个值  (niebloid)  |