std::replace_copy、std::replace_copy_if
| 定义于头文件 <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。
old_value 的元素(使用 operator==)。p 返回 true 的元素。policy 执行。true 时参与重载决议:
|
|
(C++20 前) |
|
|
(C++20 起) |
如果表达式 *first 和 new_value 中任一的结果不可写入到 d_first,那么程序非良构。
如果源范围和目标范围重叠,那么行为未定义。
参数
| first, last | - | 定义待复制元素源 范围 的迭代器对 |
| d_first | - | 目标范围的起始位置 |
| old_value | - | 待替换的元素值 |
| policy | - | 使用的 执行策略 |
| p | - |
一元谓词,当元素值应被替换时返回
true
。
表达式
p
(
v
)
必须可转换为
bool
,其中参数
|
| new_value | - | 用作替换的值 |
| 类型要求 | ||
-
InputIt
必须满足
LegacyInputIterator
的要求。
|
||
-
OutputIt
必须满足
LegacyOutputIterator
的要求。
|
||
-
ForwardIt1, ForwardIt2
必须满足
LegacyForwardIterator
的要求。
|
||
返回值
指向最后一个被复制元素之后位置的迭代器。
复杂度
给定 N 为 std:: distance ( first, last ) :
异常
带有名为
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] 的要求 |
修正为
遗留输入迭代器 |
-
↑
C++标准中的实际缺陷在于模板参数
InputIterator被错误指定为Iterator。这会影响类型要求,因为C++标准规定:对于算法库中的函数模板,名称以Iterator结尾的模板类型参数意味着需要满足相应迭代器类别的类型要求。
另见
| 将所有满足特定条件的值替换为另一个值 (函数模板 & 算法函数对象) | |
(C++20)(C++20) |
|
| 移除满足特定条件的元素 (函数模板 & 算法函数对象) | |
(C++20)(C++20) |
|
(C++20)(C++20) |
复制范围,将满足特定条件的元素替换为另一个值 (算法函数对象) |