std::replace_copy, std::replace_copy_if
来自cppreference.com
| 在标头 <algorithm> 定义
|
||
| (1) | (C++20 起为 constexpr) |
|
| |
(2) | (C++17 起) |
| (3) | ||
(C++20 起为 constexpr) (C++26 前) |
||
| |
(C++26 起) | |
| (4) | ||
| |
(C++17 起) (C++26 前) |
|
| |
(C++26 起) | |
复制来自范围 [first, last) 的元素到始于 d_first 的范围,复制过程中以 new_value 替换所有满足特定判别标准的元素。
1) 替换所有等于(用
operator== 比较)old_value 的元素。3) 替换所有谓词
p 对其满足 true 的元素。2,4) 同 (1,3),但按照
policy 执行。 这些重载只有在满足以下所有条件时才会参与重载决议:
|
|
(C++20 前) |
|
|
(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):
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}); // 可以这样做,因为 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 需要是可复制赋值 (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) |
复制范围,并将满足特定条件的元素替换为另一个值 (算法函数对象) |