首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::for_each_n

Defined in header <algorithm>

?

?

template< class InputIt, class Size, class UnaryFunction > InputIt for_each_n( InputIt first, Size n, UnaryFunction f );

(1)

(since C++17)

template< class ExecutionPolicy, class ForwardIt, class Size, class UnaryFunction2 > InputIt for_each_n( ExecutionPolicy&& policy, ForwardIt first, Size n, UnaryFunction2 f );

(2)

(since C++17)

1%29应用给定的函数对象f对范围内的每个迭代器取消引用的结果。[first, first + n),按顺序排列。

2%29应用给定的函数对象f对范围内的每个迭代器取消引用的结果。[first, first + n)%28不一定按%29顺序排列。该算法是根据policy此重载不参与过载解决,除非std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的。

对于这两个重载,如果迭代器类型是可变的,f可以通过取消引用的迭代器修改范围的元素。如果f返回一个结果,结果将被忽略。如果n小于零,则行为未定义。

参数

first

-

the beginning of the range to apply the function to

n

-

the number of elements to apply the function to

policy

-

the execution policy to use. See execution policy for details.

f

-

function object, to be applied to the result of dereferencing every iterator in the range [first, first + n) The signature of the function should be equivalent to the following: void fun(const Type &a); The signature does not need to have const &. The type Type must be such that an object of type InputIt can be dereferenced and then implicitly converted to Type. ?

类型要求

-输入必须符合输入器的要求。

---。

-单一功能必须符合可移动建筑的要求。不一定是可复制的

-单一功能2必须符合CopyConstrucable的要求。

返回值

first + n...

复杂性

一点儿没错n的应用f...

例外

带有名为ExecutionPolicy报告错误如下:

  • 如果执行作为算法一部分调用的函数,则引发异常ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。
  • 如果算法不能分配内存,std::bad_alloc被扔了。

可能的实施

模板<类Inputit,类大小,类UnaryFunction>InputIt for[医]各[医]n%28 Inputit First,size n,unaryFunction f%29{for%28 Size i=0;i<n;++first,%28void%29+i%29(f%28)%2A第一%29;}先返回;}

*。

另见

transform

applies a function to a range of elements (function template)

range-for loop

executes loop over range (since C++11)

for_each

applies a function to a range of elements (function template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com