跳到主要内容

std::ranges::for_each_n() 算法

// (1)
constexpr for_each_n_result<I, Fun>
for_each_n( I first, std::iter_difference_t<I> n, Fun f, Proj proj = {});

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

  • I - std::input_iterator
  • Fun - std::indirectly_unary_invocable<std::projected<I, Proj>>
  • Proj - (无)

Proj 模板参数的默认类型为 std::identity

辅助类型定义如下:

template< class I, class F >
using for_each_n_result = ranges::in_fun_result<I, F>;
  • (1) 按照顺序,将给定函数对象 f 应用于范围 [first, first + n) 中每个迭代器投射出的值的结果。

对于两种重载,如果迭代器类型是可变的,f 可以修改范围的元素。如果 f 返回结果,则结果将被忽略。

未定义行为

如果 n 小于零,则行为未定义

.

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

参数

first
last

要应用函数的范围的起始位置。

n

要应用函数的元素数量。

proj

应用于元素的投影。

f

函数对象,将应用于范围的每个元素。函数签名应等同于以下内容:

void fun(const Type& a);
  • 签名不需要包含 const&
  • 类型 Type 必须是这样的,即 InputIt 类型的对象可以被解引用,然后隐式转换为 Type

返回值

类型为 for_each_n_result 的对象,初始化如下:

{
first + n,
std::move(f)
}

其中 first + n 可以根据迭代器类别评估为 std::ranges::next(std::move(first), n)

复杂度

精确地执行 nfproj 的应用。

异常

(无)

可能的实现

for_each_n(1)
struct for_each_n_fn
{
template<std::input_iterator I, class Proj = std::identity,
std::indirectly_unary_invocable<std::projected<I, Proj>> Fun>
constexpr for_each_n_result<I, Fun>
operator()(I first, std::iter_difference_t<I> n, Fun fun, Proj proj = Proj{}) const
{
for (; n-- > 0; ++first)
std::invoke(fun, std::invoke(proj, *first));
return {std::move(first), std::move(fun)};
}
};

inline constexpr for_each_n_fn for_each_n {};

示例

Main.cpp
#include <algorithm>
#include <array>
#include <iostream>
#include <ranges>
#include <string_view>

struct P
{
int first;
char second;
friend std::ostream& operator<<(std::ostream& os, const P& p)
{
return os << '{' << p.first << ",'" << p.second << "'}";
}
};

auto print = [](std::string_view name, auto const& v)
{
std::cout << name << ": ";
for (auto n = v.size(); const auto& e : v)
std::cout << e << (--n ? ", " : "\n");
};

int main()
{
std::array a {1, 2, 3, 4, 5};
print("a", a);
// Negate first three numbers:
std::ranges::for_each_n(a.begin(), 3, [](auto& n) { n *= -1; });
print("a", a);

std::array s { P{1,'a'}, P{2, 'b'}, P{3, 'c'}, P{4, 'd'} };
print("s", s);
// Negate data members 'P::first' using projection:
std::ranges::for_each_n(s.begin(), 2, [](auto& x) { x *= -1; }, &P::first);
print("s", s);
// Capitalize data members 'P::second' using projection:
std::ranges::for_each_n(s.begin(), 3, [](auto& c) { c -= 'a'-'A'; }, &P::second);
print("s", s);
}
输出
a: 1, 2, 3, 4, 5
a: -1, -2, -3, 4, 5
s: {1,'a'}, {2,'b'}, {3,'c'}, {4,'d'}
s: {-1,'a'}, {-2,'b'}, {3,'c'}, {4,'d'}
s: {-1,'A'}, {-2,'B'}, {3,'C'}, {4,'d'}
本文来源于此 CppReference 页面。它可能为了改进或编辑偏好而被修改。点击“编辑此页面”查看此文档的所有更改。
悬停查看原始许可证。

std::ranges::for_each_n() 算法

// (1)
constexpr for_each_n_result<I, Fun>
for_each_n( I first, std::iter_difference_t<I> n, Fun f, Proj proj = {});

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

  • I - std::input_iterator
  • Fun - std::indirectly_unary_invocable<std::projected<I, Proj>>
  • Proj - (无)

Proj 模板参数的默认类型为 std::identity

辅助类型定义如下:

template< class I, class F >
using for_each_n_result = ranges::in_fun_result<I, F>;
  • (1) 按照顺序,将给定函数对象 f 应用于范围 [first, first + n) 中每个迭代器投射出的值的结果。

对于两种重载,如果迭代器类型是可变的,f 可以修改范围的元素。如果 f 返回结果,则结果将被忽略。

未定义行为

如果 n 小于零,则行为未定义

.

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

参数

first
last

要应用函数的范围的起始位置。

n

要应用函数的元素数量。

proj

应用于元素的投影。

f

函数对象,将应用于范围的每个元素。函数签名应等同于以下内容:

void fun(const Type& a);
  • 签名不需要包含 const&
  • 类型 Type 必须是这样的,即 InputIt 类型的对象可以被解引用,然后隐式转换为 Type

返回值

类型为 for_each_n_result 的对象,初始化如下:

{
first + n,
std::move(f)
}

其中 first + n 可以根据迭代器类别评估为 std::ranges::next(std::move(first), n)

复杂度

精确地执行 nfproj 的应用。

异常

(无)

可能的实现

for_each_n(1)
struct for_each_n_fn
{
template<std::input_iterator I, class Proj = std::identity,
std::indirectly_unary_invocable<std::projected<I, Proj>> Fun>
constexpr for_each_n_result<I, Fun>
operator()(I first, std::iter_difference_t<I> n, Fun fun, Proj proj = Proj{}) const
{
for (; n-- > 0; ++first)
std::invoke(fun, std::invoke(proj, *first));
return {std::move(first), std::move(fun)};
}
};

inline constexpr for_each_n_fn for_each_n {};

示例

Main.cpp
#include <algorithm>
#include <array>
#include <iostream>
#include <ranges>
#include <string_view>

struct P
{
int first;
char second;
friend std::ostream& operator<<(std::ostream& os, const P& p)
{
return os << '{' << p.first << ",'" << p.second << "'}";
}
};

auto print = [](std::string_view name, auto const& v)
{
std::cout << name << ": ";
for (auto n = v.size(); const auto& e : v)
std::cout << e << (--n ? ", " : "\n");
};

int main()
{
std::array a {1, 2, 3, 4, 5};
print("a", a);
// Negate first three numbers:
std::ranges::for_each_n(a.begin(), 3, [](auto& n) { n *= -1; });
print("a", a);

std::array s { P{1,'a'}, P{2, 'b'}, P{3, 'c'}, P{4, 'd'} };
print("s", s);
// Negate data members 'P::first' using projection:
std::ranges::for_each_n(s.begin(), 2, [](auto& x) { x *= -1; }, &P::first);
print("s", s);
// Capitalize data members 'P::second' using projection:
std::ranges::for_each_n(s.begin(), 3, [](auto& c) { c -= 'a'-'A'; }, &P::second);
print("s", s);
}
输出
a: 1, 2, 3, 4, 5
a: -1, -2, -3, 4, 5
s: {1,'a'}, {2,'b'}, {3,'c'}, {4,'d'}
s: {-1,'a'}, {-2,'b'}, {3,'c'}, {4,'d'}
s: {-1,'A'}, {-2,'B'}, {3,'C'}, {4,'d'}
本文来源于此 CppReference 页面。它可能为了改进或编辑偏好而被修改。点击“编辑此页面”查看此文档的所有更改。
悬停查看原始许可证。