跳到主要内容

std::ranges::replace_copy_if() 算法

// (1)
constexpr replace_copy_if_result<I, O>
replace_copy_if( I first, S last, O result, Pred pred, const T& new_value,
Proj proj = {} );

// (2)
constexpr replace_copy_if_result<ranges::borrowed_iterator_t<R>, O>
replace_copy_if( R&& r, O result, Pred pred, const T& new_value,
Proj proj = {} );

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

  • I - std::input_iterator
  • S - std::sentinel_for<I>
  • O - std::output_iterator<const T& >
  • R - std::input_range
  • Proj - (无)
  • T - (无)
  • 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_copyable<I, O>
  • (2) - std::indirectly_copyable<ranges::iterator_t<R>, O>

辅助类型定义如下:

template< class I, class O >
using replace_copy_if_result = ranges::in_out_result<I, O>;
  • (1) 将源范围 [first; last) 中的元素复制到从 result 开始的目标范围,将所有满足谓词 pred 的元素替换为 new_value

    使用 std::invoke(pred, std::invoke(proj, *(first + (i - result)))) == true 进行比较。

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

未定义行为

行为未定义

如果源范围和目标范围重叠。

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

参数

first
last

要复制的元素范围。

r

要复制的元素范围。

result

目标范围的开头。

old_value

要替换的值。

pred

一元谓词,如果元素应被忽略,则返回true

proj

要应用于元素的投影。

返回值

{
last,
result + N
}

其中 N 是源范围的大小。

复杂度

给定 N 为

  • (1) - ranges::distance(first, last)
  • (2) - ranges::distance(r)

对应谓词 pred 和任何投影 proj 的精确 N 次应用。

异常

(无)

可能的实现

replace_copy_if(1)
struct replace_copy_if_fn
{
template<std::input_iterator I, std::sentinel_for<I> S, class T,
std::output_iterator<const T&> O,
class Proj = std::identity, std::indirect_unary_predicate<
std::projected<I, Proj>> Pred>
requires std::indirectly_copyable<I, O>
constexpr ranges::replace_copy_if_result<I, O>
operator()(I first, S last, O result, Pred pred, const T& new_value,
Proj proj = {}) const
{
for (; first != last; ++first, ++result)
*result = std::invoke(pred, std::invoke(proj, *first)) ? new_value : *first;
return {std::move(first), std::move(result)};
}

template<ranges::input_range R, class T, std::output_iterator<const T&> O,
class Proj = std::identity,
std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>> Pred>
requires std::indirectly_copyable<ranges::iterator_t<R>, O>
constexpr ranges::replace_copy_if_result<ranges::borrowed_iterator_t<R>, O>
operator()(R&& r, O result, Pred pred, const T& new_value,
Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r), std::move(result),
std::move(pred), new_value, std::move(proj));
}
};

inline constexpr replace_copy_if_fn replace_copy_if {};

示例

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

std::ranges::replace_copy_if() 算法

// (1)
constexpr replace_copy_if_result<I, O>
replace_copy_if( I first, S last, O result, Pred pred, const T& new_value,
Proj proj = {} );

// (2)
constexpr replace_copy_if_result<ranges::borrowed_iterator_t<R>, O>
replace_copy_if( R&& r, O result, Pred pred, const T& new_value,
Proj proj = {} );

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

  • I - std::input_iterator
  • S - std::sentinel_for<I>
  • O - std::output_iterator<const T& >
  • R - std::input_range
  • Proj - (无)
  • T - (无)
  • 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_copyable<I, O>
  • (2) - std::indirectly_copyable<ranges::iterator_t<R>, O>

辅助类型定义如下:

template< class I, class O >
using replace_copy_if_result = ranges::in_out_result<I, O>;
  • (1) 将源范围 [first; last) 中的元素复制到从 result 开始的目标范围,将所有满足谓词 pred 的元素替换为 new_value

    使用 std::invoke(pred, std::invoke(proj, *(first + (i - result)))) == true 进行比较。

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

未定义行为

行为未定义

如果源范围和目标范围重叠。

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

参数

first
last

要复制的元素范围。

r

要复制的元素范围。

result

目标范围的开头。

old_value

要替换的值。

pred

一元谓词,如果元素应被忽略,则返回true

proj

要应用于元素的投影。

返回值

{
last,
result + N
}

其中 N 是源范围的大小。

复杂度

给定 N 为

  • (1) - ranges::distance(first, last)
  • (2) - ranges::distance(r)

对应谓词 pred 和任何投影 proj 的精确 N 次应用。

异常

(无)

可能的实现

replace_copy_if(1)
struct replace_copy_if_fn
{
template<std::input_iterator I, std::sentinel_for<I> S, class T,
std::output_iterator<const T&> O,
class Proj = std::identity, std::indirect_unary_predicate<
std::projected<I, Proj>> Pred>
requires std::indirectly_copyable<I, O>
constexpr ranges::replace_copy_if_result<I, O>
operator()(I first, S last, O result, Pred pred, const T& new_value,
Proj proj = {}) const
{
for (; first != last; ++first, ++result)
*result = std::invoke(pred, std::invoke(proj, *first)) ? new_value : *first;
return {std::move(first), std::move(result)};
}

template<ranges::input_range R, class T, std::output_iterator<const T&> O,
class Proj = std::identity,
std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>> Pred>
requires std::indirectly_copyable<ranges::iterator_t<R>, O>
constexpr ranges::replace_copy_if_result<ranges::borrowed_iterator_t<R>, O>
operator()(R&& r, O result, Pred pred, const T& new_value,
Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r), std::move(result),
std::move(pred), new_value, std::move(proj));
}
};

inline constexpr replace_copy_if_fn replace_copy_if {};

示例

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