std::replace, std::replace_if
在标头 <algorithm> 定义
|
||
(1) | ||
template< class ForwardIt, class T > void replace( ForwardIt first, ForwardIt last, |
(C++20 起为 constexpr ) (C++26 前) |
|
template< class ForwardIt, class T = typename std::iterator_traits <ForwardIt>::value_type > |
(C++26 起) | |
(2) | ||
template< class ExecutionPolicy, class ForwardIt, class T > void replace( ExecutionPolicy&& policy, |
(C++17 起) (C++26 前) |
|
template< class ExecutionPolicy, class ForwardIt, class T = typename std::iterator_traits |
(C++26 起) | |
(3) | ||
template< class ForwardIt, class UnaryPred, class T > void replace_if( ForwardIt first, ForwardIt last, |
(C++20 起为 constexpr ) (C++26 前) |
|
template< class ForwardIt, class UnaryPred, class T = typename std::iterator_traits |
(C++26 起) | |
(4) | ||
template< class ExecutionPolicy, class ForwardIt, class UnaryPred, class T > |
(C++17 起) (C++26 前) |
|
template< class ExecutionPolicy, class ForwardIt, class UnaryPred, |
(C++26 起) | |
以 new_value 替换范围 [
first,
last)
中所有满足特定判别标准的元素。
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> |
(C++20 前) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> |
(C++20 起) |
如果 *first = new_value 非法 (C++20 前)new_value 不可写入 first (C++20 起),那么程序非良构。
参数
first, last | - | 要处理的元素范围 |
old_value | - | 要被替换的元素值 |
policy | - | 所用的执行策略。细节见执行策略。 |
p | - | 如果应该替换元素则返回 true 的一元谓词。 对每个(可为 const 的) |
new_value | - | 用作替换者的值 |
类型要求 | ||
-ForwardIt 必须满足老式向前迭代器 (LegacyForwardIterator) 。
| ||
-UnaryPred 必须满足谓词 (Predicate) 。
|
返回值
(无)
复杂度
给定 N 为 std::distance(first, last):
异常
拥有名为 ExecutionPolicy
的模板形参的重载按下列方式报告错误:
- 如果作为算法一部分调用的函数的执行抛出异常,且
ExecutionPolicy
是标准策略之一,那么调用 std::terminate。对于任何其他ExecutionPolicy
,行为由实现定义。 - 如果算法无法分配内存,那么抛出 std::bad_alloc。
注解
算法以引用接收 old_value 和 new_value,所以如果其中任何一个是到范围 [
first,
last)
中元素的引用,那么算法的行为可能会与预期不同。
功能特性测试宏 | 值 | 标准 | 功能特性 |
---|---|---|---|
__cpp_lib_algorithm_default_value_type |
202403 | (C++26) | 算法中的列表初始化 (1-4) |
可能的实现
replace |
---|
template<class ForwardIt, class T = typename std::iterator_traits<ForwardIt>::value_type> void replace(ForwardIt first, ForwardIt last, const T& old_value, const T& new_value) { for (; first != last; ++first) if (*first == old_value) *first = new_value; } |
replace_if |
template<class ForwardIt, class UnaryPred, class T = typename std::iterator_traits<ForwardIt>::value_type> void replace_if(ForwardIt first, ForwardIt last, UnaryPred p, const T& new_value) { for (; first != last; ++first) if (p(*first)) *first = new_value; } |
示例
#include <algorithm> #include <array> #include <complex> #include <functional> #include <iostream> void println(const auto& seq) { for (const auto& e : seq) std::cout << e << ' '; std::cout << '\n'; } int main() { std::array<int, 10> s{5, 7, 4, 2, 8, 6, 1, 9, 0, 3}; // 将出现的所有 8 替换为 88。 std::replace(s.begin(), s.end(), 8, 88); println(s); // 将所有小于 5 的值替换为 55。 std::replace_if(s.begin(), s.end(), std::bind(std::less<int>(), std::placeholders::_1, 5), 55); println(s); std::array<std::complex<double>, 2> nums{{{1, 3}, {1, 3}}}; #ifdef __cpp_lib_algorithm_default_value_type std::replace(nums.begin(), nums.end(), {1, 3}, {4, 2}); #else std::replace(nums.begin(), nums.end(), std::complex<double>{1, 3}, std::complex<double>{4, 2}); #endif println(nums); }
输出:
5 7 4 2 88 6 1 9 0 3 5 7 55 55 88 6 55 9 55 55 (4,2) (4,2)
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
LWG 283 | C++98 | T 需要是可复制赋值 (CopyAssignable) 的(对于 replace 还需要是可相等比较 (EqualityComparable) 的),但是 ForwardIt 的值类型不一定是 T ,T 也不一定可写入 ForwardIt
|
改成要求 *first = new_value 合法 |
参阅
复制一个范围,并将满足特定判别标准的元素替换为另一个值 (函数模板) | |
(C++20)(C++20) |
将所有满足特定判别标准的值替换为另一个值 (niebloid) |