std::ranges::replace_copy() 算法
- 自 C++20 起
- 简化
- 详细
// (1)
constexpr replace_copy_result<I, O>
replace_copy( I first, S last, O result, const T1& old_value, const T2& new_value,
Proj proj = {} );
// (2)
constexpr replace_copy_result<ranges::borrowed_iterator_t<R>, O>
replace_copy( R&& r, O result, const T1& old_value, const T2& new_value,
Proj proj = {} );
参数类型是泛型的,并具有以下约束
I
-std::input_iterator
S
-std::sentinel_for<I>
O
-std::output_iterator<const T2&>
R
-std::input_range
T1
- (无)T2
- (无)Proj
- (无)
对于所有重载,Proj
模板参数的默认类型为 std::identity
。
此外,每个重载都有以下约束
- (1):
indirectly_copyable<I, O>
&& indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T1*> - (2):
indirectly_copyable<ranges::iterator_t<R>, O>
&& indirect_binary_predicate<ranges::equal_to, projected<ranges::iterator_t<R>, Proj>, const T1*>
(为方便阅读,此处省略了 std::
命名空间)
// (1)
template<
std::input_iterator I,
std::sentinel_for<I> S,
class T1,
class T2,
std::output_iterator<const T2&> O,
class Proj = std::identity
>
requires std::indirectly_copyable<I, O> &&
std::indirect_binary_predicate<ranges::equal_to,
std::projected<I, Proj>, const T1*>
constexpr replace_copy_result<I, O>
replace_copy( I first, S last, O result, const T1& old_value, const T2& new_value,
Proj proj = {} );
// (2)
template<
ranges::input_range R,
class T1,
class T2,
std::output_iterator<const T2&> O,
class Proj = std::identity
>
requires std::indirectly_copyable<ranges::iterator_t<R>, O> &&
std::indirect_binary_predicate<ranges::equal_to,
std::projected<ranges::iterator_t<R>, Proj>, const T1*>
constexpr replace_copy_result<ranges::borrowed_iterator_t<R>, O>
replace_copy( R&& r, O result, const T1& old_value, const T2& new_value,
Proj proj = {} );
辅助类型定义如下:
template< class I, class O >
using replace_copy_result = ranges::in_out_result<I, O>;
-
(1) 将元素从源范围 [
first
;last
) 复制到从result
开始的目标范围,并将所有等于old_value
的元素替换为new_value
。使用
std::invoke(proj, *(first + (i - result)))
进行比较。 -
(2) 与 (1) 相同,但使用
r
作为源范围,如同使用ranges::begin(r)
作为first
和ranges::end(r)
作为last
。
行为未定义
如果源范围和目标范围重叠。本页描述的函数类实体是niebloids。
参数
first last | 要复制的元素范围。 |
r | 要复制的元素范围。 |
result | 目标范围的开头。 |
old_value | 要替换的元素的值。 |
new_value | 用作替换的值。 |
proj | 要应用于元素的投影。 |
返回值
{
last,
result + N
}
其中 N 是源范围的大小。
复杂度
给定 N 为
- (1) -
ranges::distance(first, last)
- (2) -
ranges::distance(r)
在用 proj
投影后,精确进行 N 次比较。
异常
(无)
可能的实现
replace_copy(1) 和 replace_copy(2)
struct replace_copy_fn
{
template<std::input_iterator I, std::sentinel_for<I> S, class T1, class T2,
std::output_iterator<const T2&> O, class Proj = std::identity>
requires std::indirectly_copyable<I, O> &&
std::indirect_binary_predicate<ranges::equal_to,
std::projected<I, Proj>, const T1*>
constexpr ranges::replace_copy_result<I, O>
operator()(I first, S last, O result, const T1& old_value, const T2& new_value,
Proj proj = {}) const
{
for (; first != last; ++first, ++result)
*result = (std::invoke(proj, *first) == old_value) ? new_value : *first;
return {std::move(first), std::move(result)};
}
template<ranges::input_range R, class T1, class T2,
std::output_iterator<const T2&> O, class Proj = std::identity>
requires std::indirectly_copyable<ranges::iterator_t<R>, O> &&
std::indirect_binary_predicate<ranges::equal_to,
std::projected<ranges::iterator_t<R>, Proj>, const T1*>
constexpr ranges::replace_copy_result<ranges::borrowed_iterator_t<R>, O>
operator()(R&& r, O result, const T1& old_value, const T2& new_value,
Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r), std::move(result),
old_value, new_value, std::move(proj));
}
};
inline constexpr replace_copy_fn replace_copy {};
示例
#include <algorithm>
#include <array>
#include <iostream>
#include <vector>
int main()
{
auto print = [](const auto rem, const auto& v)
{
for (std::cout << rem << ": "; const auto& e : v)
std::cout << e << ' ';
std::cout << '\n';
};
std::vector<int> o;
std::array p {1, 6, 1, 6, 1, 6};
o.resize(p.size());
print("p", p);
std::ranges::replace_copy(p, o.begin(), 6, 9);
print("o", o);
std::array q {1, 2, 3, 6, 7, 8, 4, 5};
o.resize(q.size());
print("q", q);
std::ranges::replace_copy_if(q, o.begin(), [](int x) { return 5 < x; }, 5);
print("o", o);
}
p: 1 6 1 6 1 6
o: 1 9 1 9 1 9
q: 1 2 3 6 7 8 4 5
o: 1 2 3 5 5 5 4 5