std::replace, std::replace_if
定义于头文件 <algorithm> |
||
(1) | ||
template< class ForwardIt, class T > void replace( ForwardIt first, ForwardIt last, |
(constexpr since C++20) (until C++26) |
|
template< class ForwardIt, class T = typename std::iterator_traits <ForwardIt>::value_type > |
(since C++26) | |
(2) | ||
template< class ExecutionPolicy, class ForwardIt, class T > void replace( ExecutionPolicy&& policy, |
(since C++17) (until C++26) |
|
template< class ExecutionPolicy, class ForwardIt, class T = typename std::iterator_traits |
(since C++26) | |
(3) | ||
template< class ForwardIt, class UnaryPred, class T > void replace_if( ForwardIt first, ForwardIt last, |
(constexpr since C++20) (until C++26) |
|
template< class ForwardIt, class UnaryPred, class T = typename std::iterator_traits |
(since C++26) | |
(4) | ||
template< class ExecutionPolicy, class ForwardIt, class UnaryPred, class T > |
(since C++17) (until C++26) |
|
template< class ExecutionPolicy, class ForwardIt, class UnaryPred, |
(since C++26) | |
如果范围 [
first,
last)
中的所有元素满足特定条件,则将其替换为 new_value。
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true。 |
(until C++20) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> 为 true。 |
(since C++20) |
如果 *first = new_value 无效(until C++20)new_value 不可写入到 first(since C++20),则程序是非良构的。
内容 |
[编辑] 参数
first, last | - | 定义要处理元素范围的迭代器对 |
old_value | - | 要替换的元素的值 |
policy | - | 要使用的执行策略 |
p | - | 一元谓词,如果元素值应被替换,则返回 true。 表达式 p(v) 必须可转换为 bool,对于类型(可能是 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)
的元素的引用,则它可能具有意外的行为。
Feature-test 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_algorithm_default_value_type |
202403 |
(C++26) | 列表初始化 用于算法 (1-4) |
[编辑] 可能的实现
replace (1) |
---|
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 (3) |
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}; // Replace all occurrences of 8 with 88. std::replace(s.begin(), s.end(), 8, 88); println(s); // Replace all values less than 5 with 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++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 283 | C++98 | T 被要求是 CopyAssignable (和 EqualityComparable对于 replace ),但 ForwardIt 的值类型不是始终是 T 并且 T 并不总是可写入到 ForwardIt |
要求 *first = new_value 改为有效 |
[编辑] 参见
复制一个范围,将满足特定条件的元素替换为另一个值 (函数模板) | |
(C++20)(C++20) |
将所有满足特定条件的值替换为另一个值 (算法函数对象) |