for_each
std::for_each
applies a unary callable to each element of its range. The range is given by the input iterators.
UnFunc
std
::
for_each
(
InpIt
first
,
InpIt
second
,
UnFunc
func
)
void
std
::
for_each
(
ExePol
pol
,
FwdIt
first
,
FwdIt
second
,
UnFunc
func
)
std::for_each
when used without an explicit execution policy is a special algorithm because it returns its callable argument. If you invoke std::for_each
with a function object, you can store the result of the function call directly in the function object.
InpIt
std
::
for_each_n
(
InpIt
first
,
Size
n
,
UnFunc
func
)
FwdIt
std
::
for_each_n
(
ExePol
pol
,
FwdIt
first
,
Size
n
,
UnFunc
func
)
std::for_each_n
is new with C++17 and applies a unary callable to the first n elements of its range. The range is given by an input iterator and a size.
std::for_each
// forEach.cpp
...
#include
<algorithm>
...
template
<
typename
T
>
class
ContInfo
{
public
:
void
operator
()(
T
t
){
num
++
;
sum
+=
t
;
}
int
getSum
()
const
{
...