std::ranges::for_each_n, std::ranges::for_each_n_result
来自 zh.cppreference.net
| 定义于头文件 <algorithm>
|
||
| 调用签名 |
||
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>
for_each_n( I first, std::iter_difference_t<I> count,
Fun f, Proj proj = {} );
|
(1) | (C++20 起) |
template< /*execution-policy*/ Ep,
std::random_access_iterator I, class Proj = identity,
std::indirectly_unary_invocable<std::projected<I, Proj>> Fun >
I for_each_n( Ep&& policy, I first, iter_difference_t<I> count,
Fun f, Proj proj = {} );
|
(2) | (C++26 起) |
| 辅助类型 |
||
template< class I, class F >
using for_each_n_result = ranges::in_fun_result<I, F>;
|
(3) | (C++20 起) |
有关 /*execution-policy*/ 的定义,请参见此页面.
向目标范围 f 中的每个元素(通过 proj 投影)应用给定的可调用对象 [first, ranges::next(first, count))。如果 f 返回一个结果,则该结果被忽略。
1)
f 按顺序从 first 开始应用。2)
f 可能不按顺序应用。算法按照 policy 执行。 与其他并行算法不同,
for_each_n 不允许对目标范围中的元素进行任意复制。如果 count >= 0 不是 true,则行为未定义。
本页描述的类似函数实体是算法函数对象(非正式地称为niebloids),这意味着:
参数
| first | - | 目标范围的起始位置 |
| count | - | 目标范围中的元素数量 |
| f | - | 应用于(投影后)元素的可调用对象 |
| proj | - | 应用于元素的投影函数 |
| policy | - | 要使用的执行策略 |
返回值
1)
{ranges::next(first, count), std::move(f)}2)
ranges::next(first, count)复杂度
正好 count 次调用 f 和 proj。
异常
2) 在执行过程中:
- 如果并行化所需的临时内存资源不可用,则会抛出std::bad_alloc异常。
- 如果通过算法参数访问对象时抛出未捕获的异常,其行为由执行策略决定(对于标准策略,会调用std::terminate)。
注解
若投影返回可变引用,则f可能修改目标范围中的元素。
可能的实现
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> count,
Fun fun, Proj proj = Proj{}) const
{
for (; count-- > 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{};
示例
运行此代码
#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, const auto& 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'}
参阅
(C++17) |
将一个函数对象应用于序列的前 N 个元素 (函数模板) |
| 将一元函数对象应用于范围 中的元素(函数模板 & 算法函数对象) | |
(C++20) |
|
范围-for循环(C++11)
|
在范围内执行循环 |