跳到主要内容

std::ranges::replace() 算法

// (1)
constexpr I
replace( I first, S last, const T1& old_value, const T2& new_value,
Proj proj = {} );

// (2)
constexpr ranges::borrowed_iterator_t<R>
replace( R&& r, const T1& old_value, const T2& new_value, Proj proj = {} );

参数类型是泛型的,并具有以下约束

  • I - std::permutable
  • S - std::sentinel_for<I>
  • R - std::ranges::input_range
  • T1 - (无)
  • T2 - (无)
  • Proj - (无)

对于所有重载,Proj 模板参数的默认类型为 std::identity

此外,每个重载都有以下约束

  • (1):
    indirectly_writable<I, const T2&>
    && indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T1*>
  • (2):
    indirectly_writable<ranges::iterator_t<R>, const T2&>
    && indirect_binary_predicate<ranges::equal_to, projected<ranges::iterator_t<R>, Proj>, const T1*>

(为方便阅读,此处省略了 std:: 命名空间)

  • (1) 使用 std::invoke(proj, *i) == old_value 进行比较,将所有等于 old_value 的元素替换为 new_value

  • (2)(1) 相同,但使用 r 作为源范围,如同使用 ranges::begin(r) 作为 firstranges::end(r) 作为 last

本页描述的函数类实体是niebloids

参数

first
last

要处理的元素范围。

r

要处理的元素范围。

old_value

要搜索和替换的值。

new_value

用作替换的值。

proj

应用于元素的投影。

返回值

一个等于 last 的迭代器。

复杂度

精确地对投影 proj 进行 ranges::distance(first, last) 次应用。

异常

(无)

可能的实现

replace(1)
struct replace_fn
{
template<std::input_iterator I, std::sentinel_for<I> S, class T1, class T2,
class Proj = std::identity>
requires std::indirectly_writable<I, const T2&> && std::indirect_binary_predicate<
ranges::equal_to, std::projected<I, Proj>, const T1*>
constexpr I
operator()(I first, S last, const T1& old_value, const T2& new_value,
Proj proj = {}) const
{
for (; first != last; ++first)
if (old_value == std::invoke(proj, *first))
*first = new_value;
return first;
}

template<ranges::input_range R, class T1, class T2, class Proj = std::identity>
requires std::indirectly_writable<ranges::iterator_t<R>, const T2&> &&
std::indirect_binary_predicate<ranges::equal_to,
std::projected<ranges::iterator_t<R>, Proj>, const T1*>
constexpr ranges::borrowed_iterator_t<R>
operator()(R&& r, const T1& old_value, const T2& new_value, Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r), old_value,
new_value, std::move(proj));
}
};

inline constexpr replace_fn replace {};

备注

因为该算法通过引用接受 old_valuenew_value,如果它们中的任何一个是范围 [first; last) 中元素的引用,则可能会出现意外行为。

示例

Main.cpp

#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
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::ranges::replace() 算法

// (1)
constexpr I
replace( I first, S last, const T1& old_value, const T2& new_value,
Proj proj = {} );

// (2)
constexpr ranges::borrowed_iterator_t<R>
replace( R&& r, const T1& old_value, const T2& new_value, Proj proj = {} );

参数类型是泛型的,并具有以下约束

  • I - std::permutable
  • S - std::sentinel_for<I>
  • R - std::ranges::input_range
  • T1 - (无)
  • T2 - (无)
  • Proj - (无)

对于所有重载,Proj 模板参数的默认类型为 std::identity

此外,每个重载都有以下约束

  • (1):
    indirectly_writable<I, const T2&>
    && indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T1*>
  • (2):
    indirectly_writable<ranges::iterator_t<R>, const T2&>
    && indirect_binary_predicate<ranges::equal_to, projected<ranges::iterator_t<R>, Proj>, const T1*>

(为方便阅读,此处省略了 std:: 命名空间)

  • (1) 使用 std::invoke(proj, *i) == old_value 进行比较,将所有等于 old_value 的元素替换为 new_value

  • (2)(1) 相同,但使用 r 作为源范围,如同使用 ranges::begin(r) 作为 firstranges::end(r) 作为 last

本页描述的函数类实体是niebloids

参数

first
last

要处理的元素范围。

r

要处理的元素范围。

old_value

要搜索和替换的值。

new_value

用作替换的值。

proj

应用于元素的投影。

返回值

一个等于 last 的迭代器。

复杂度

精确地对投影 proj 进行 ranges::distance(first, last) 次应用。

异常

(无)

可能的实现

replace(1)
struct replace_fn
{
template<std::input_iterator I, std::sentinel_for<I> S, class T1, class T2,
class Proj = std::identity>
requires std::indirectly_writable<I, const T2&> && std::indirect_binary_predicate<
ranges::equal_to, std::projected<I, Proj>, const T1*>
constexpr I
operator()(I first, S last, const T1& old_value, const T2& new_value,
Proj proj = {}) const
{
for (; first != last; ++first)
if (old_value == std::invoke(proj, *first))
*first = new_value;
return first;
}

template<ranges::input_range R, class T1, class T2, class Proj = std::identity>
requires std::indirectly_writable<ranges::iterator_t<R>, const T2&> &&
std::indirect_binary_predicate<ranges::equal_to,
std::projected<ranges::iterator_t<R>, Proj>, const T1*>
constexpr ranges::borrowed_iterator_t<R>
operator()(R&& r, const T1& old_value, const T2& new_value, Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r), old_value,
new_value, std::move(proj));
}
};

inline constexpr replace_fn replace {};

备注

因为该算法通过引用接受 old_valuenew_value,如果它们中的任何一个是范围 [first; last) 中元素的引用,则可能会出现意外行为。

示例

Main.cpp

#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
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。