std::replace, std::replace_if
| 受约束算法及范围上的算法 (C++20) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 受约束算法: std::ranges::copy, std::ranges::sort, ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 执行策略 (C++17) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 不修改序列的操作 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 修改序列的操作 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Partitioning operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 划分操作 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 排序操作 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 二分搜索操作 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 集合操作(在已排序范围上) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 堆操作 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 最小/最大操作 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 排列 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 数值运算 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 未初始化存储上的操作 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| C 库 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
在标头
<algorithm>
定义
|
||
| (1) | ||
|
template< class ForwardIt, class T >
void replace( ForwardIt
first, ForwardIt last, |
(C++20 前) | |
|
template< class ForwardIt, class T >
constexpr void
replace( ForwardIt first, ForwardIt last, |
(C++20 起) | |
|
template< class ExecutionPolicy,
class ForwardIt, class T
>
void replace(
ExecutionPolicy&& policy, ForwardIt
first, ForwardIt last, |
(2) | (C++17 起) |
| (3) | ||
|
template< class ForwardIt, class UnaryPredicate, class
T >
void replace_if(
ForwardIt first, ForwardIt last, |
(C++20 前) | |
|
template< class ForwardIt, class UnaryPredicate, class
T >
constexpr void
replace_if( ForwardIt first, ForwardIt
last, |
(C++20 起) | |
|
template< class ExecutionPolicy,
class ForwardIt,
class UnaryPredicate, class T > |
(4) | (C++17 起) |
以 new_value 替换范围
[first, last) 中所有满足特定判别标准的元素。
operator== 比较)old_value 的元素。|
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> |
(C++20 前) |
|
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> |
(C++20 起) |
参数
| first, last | - | 要处理的元素范围 |
| old_value | - | 要被替换的元素值 |
| policy | - | 所用的执行策略。细节见执行策略。 |
| p | - | 如果应该替换元素则返回 true 的一元谓词。 对每个(可为 const 的) |
| new_value | - | 用作替换者的值 |
| 类型要求 | ||
-ForwardIt 必须符合老式向前迭代器 (LegacyForwardIterator)
的要求。
|
||
-UnaryPredicate 必须符合谓词 (Predicate)
的要求。
|
||
|
表达式 *first = new_value 必须合法。 |
(C++20 前) |
|
new_value 必须可写入 first。 |
(C++20 起) |
返回值
(无)
复杂度
给定 N 为 std::distance(first,
last):
N 次 operator== 与
old_value 进行比较N 次谓词 p异常
拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:
- 如果作为算法一部分调用的函数的执行抛出异常,且
ExecutionPolicy是标准策略之一,那么调用 std::terminate。对于任何其他ExecutionPolicy,行为由实现定义。 - 如果算法无法分配内存,那么抛出 std::bad_alloc。
注解
算法以引用接收 old_value 和 new_value,所以如果其中有一个是到范围
[first, last) 中元素的引用,那么算法的行为可能会与预期不同。
可能的实现
| replace |
|---|
template<class ForwardIt, class T> 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 UnaryPredicate, class T> void replace_if(ForwardIt first, ForwardIt last, UnaryPredicate p, const T& new_value) { for (; first != last; ++first) if (p(*first)) *first = new_value; } |
示例
下列代码首先在存储整数的 vector 中以 88 替换所有 8,然后以 55 替换所有小于 5 的值。
#include <algorithm> #include <array> #include <iostream> #include <functional> int main() { std::array<int, 10> s{5, 7, 4, 2, 8, 6, 1, 9, 0, 3}; std::replace(s.begin(), s.end(), 8, 88); for (int a : s) std::cout << a << " "; std::cout << '\n'; std::replace_if(s.begin(), s.end(), std::bind(std::less<int>(), std::placeholders::_1, 5), 55); for (int a : s) std::cout << a << " "; std::cout << '\n'; }
输出:
5 7 4 2 88 6 1 9 0 3 5 7 55 55 88 6 55 9 55 55
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 283 | C++98 | T 需要是 可复制赋值
(CopyAssignable)
的(对于 replace还需要是 可相等比较 (EqualityComparable) 的),但是 ForwardIt 的值类型不一定是 T,T 也不一定可写入
ForwardIt
|
改成要求 *first = new_value 合法 |
参阅
| 复制一个范围内的元素,并将满足特定判别标准的元素替换为另一个值 (函数模板) |
|
|
(C++20)(C++20)
|
将所有满足特定判别标准的值替换为另一个值 (niebloid) |