std::ranges::replace_if() 算法
- 自 C++20 起
- 简化
- 详细
// (1)
constexpr I
replace_if( I first, S last, Pred pred, const T& new_value, Proj proj = {} );
// (2)
constexpr ranges::borrowed_iterator_t<R>
replace_if( R&& r, Pred pred, const T& new_value, Proj proj = {} );
参数类型是泛型的,并具有以下约束
I
-std::input_iterator
S
-std::sentinel_for<I>
R
-std::ranges::input_range
T
- (无)Proj
- (无)Pred
:- (1) -
std::indirect_unary_predicate<std::projected<I, Proj>>
- (2) -
std::indirect_unary_predicate< std::projected<ranges::iterator_t<R>, Proj>>
对于所有重载,Proj
模板参数的默认类型为 std::identity
。
此外,每个重载都有以下约束
- (1) -
std::indirectly_writable<I, const T&>
- (2) -
std::indirectly_writable<ranges::iterator_t<R>, const T&>
(为方便阅读,此处省略了 std::
命名空间)
// (1)
template<
std::input_iterator I,
std::sentinel_for<I> S,
class T,
class Proj = std::identity,
std::indirect_unary_predicate<std::projected<I, Proj>> Pred
>
requires std::indirectly_writable<I, const T&>
constexpr I
replace_if( I first, S last, Pred pred, const T& new_value, Proj proj = {} );
// (2)
template<
ranges::input_range R,
class T, class Proj = std::identity,
std::indirect_unary_predicate< std::projected<ranges::iterator_t<R>, Proj>> Pred
>
requires std::indirectly_writable<ranges::iterator_t<R>, const T&>
constexpr ranges::borrowed_iterator_t<R>
replace_if( R&& r, Pred pred, const T& new_value, Proj proj = {} );
-
(1) 替换所有谓词
pred
计算结果为true
的元素,其中计算表达式为std::invoke(pred, std::invoke(proj, *i))
。 -
(2) 与 (1) 相同,但使用
r
作为源范围,如同使用ranges::begin(r)
作为first
和ranges::end(r)
作为last
。
通过移动赋值的方式移动范围内的元素以进行移除,使无需替换的元素出现在范围的开头。
保留其余元素的相对顺序,且容器的物理大小不变。
指向范围新*逻辑*末尾和*物理*末尾之间元素的迭代器仍然可解引用,但元素本身的值未指定(根据MoveAssignable后置条件)。 (自 C++11 起)
本页描述的函数类实体是niebloids。
参数
first last | 要处理的元素范围。 |
r | 要处理的元素范围。 |
new_value | 用作替换的值。 |
proj | 应用于元素的投影。 |
pred | 一元谓词,如果元素值应该被替换,则返回 |
返回值
一个等于 last
的迭代器。
复杂度
精确地应用 ranges::distance(first, last)
次相应的谓词 pred
和任何投影 proj
。
异常
(无)
可能的实现
replace_if(1)
struct replace_if_fn
{
template<std::input_iterator I, std::sentinel_for<I> S, class T,
class Proj = std::identity, std::indirect_unary_predicate<
std::projected<I, Proj>> Pred>
requires std::indirectly_writable<I, const T&>
constexpr I
operator()(I first, S last, Pred pred, const T& new_value, Proj proj = {}) const
{
for (; first != last; ++first)
if (!!std::invoke(pred, std::invoke(proj, *first)))
*first = new_value;
return std::move(first);
}
template<ranges::input_range R, class T, class Proj = std::identity,
std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>> Pred>
requires std::indirectly_writable<ranges::iterator_t<R>, const T&>
constexpr ranges::borrowed_iterator_t<R>
operator()(R&& r, Pred pred, const T& new_value, Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r), std::move(pred),
new_value, std::move(proj));
}
};
inline constexpr replace_if_fn replace_if {};
备注
由于算法通过引用获取 old_value
和 new_value
,如果其中任何一个是对范围 [first
; last
) 中元素的引用,则可能会出现意外行为。
示例
#include <algorithm>
#include <array>
#include <iostream>
int main()
{
auto print = [](const auto& v)
{
for (const auto& e : v)
std::cout << e << ' ';
std::cout << '\n';
};
std::array p {1, 6, 1, 6, 1, 6};
print(p);
std::ranges::replace(p, 6, 9);
print(p);
std::array q {1, 2, 3, 6, 7, 8, 4, 5};
print(q);
std::ranges::replace_if(q, [](int x) { return 5 < x; }, 5);
print(q);
}
1 6 1 6 1 6
1 9 1 9 1 9
1 2 3 6 7 8 4 5
1 2 3 5 5 5 4 5