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, |
(1) | (C++20 起为 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class T > |
(2) | (C++17 起) |
(3) | ||
template< class InputIt, class OutputIt, class UnaryPred, class T > OutputIt replace_copy_if |
(C++20 起为 constexpr) (直到 C++26) |
|
template< class InputIt, class OutputIt, class UnaryPred, class T = typename std::iterator_traits |
(C++26 起) | |
(4) | ||
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryPred, class T > |
(C++17 起) (直到 C++26) |
|
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryPred, class T = typename std::iterator_traits |
(C++26 起) | |
将范围 [
first,
last)
中的元素复制到从 d_first 开始的另一个范围,同时将所有满足特定条件的元素替换为 new_value。
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。 对于类型(可能是 const) |
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}); // 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(传统输入迭代器) |
- ↑ C++ 标准中的实际缺陷是模板参数
InputIterator
被错误指定为Iterator
。这会影响类型要求,因为 C++ 标准规定对于算法库中的函数模板,其名称以Iterator
结尾的模板类型参数暗示了相应迭代器类别的类型要求。
[编辑] 另请参阅
用另一个值替换所有满足特定条件的值 (函数模板) | |
移除满足特定标准的元素 (函数模板) | |
(C++20)(C++20) |
复制一个范围,同时用另一个值替换满足特定条件的元素 (算法函数对象) |