25 Algorithms library [algorithms]

25.6 Non-modifying sequence operations [alg.nonmodifying]

25.6.4 For each [alg.foreach]

template<class InputIterator, class Function> constexpr Function for_each(InputIterator first, InputIterator last, Function f);
Preconditions: Function meets the Cpp17MoveConstructible requirements (Table 28).
Note
:
Function need not meet the requirements of Cpp17CopyConstructible (Table 29).
— end note
 ]
Effects: Applies f to the result of dereferencing every iterator in the range [first, last), starting from first and proceeding to last - 1.
Note
:
If the type of first meets the requirements of a mutable iterator, f may apply non-constant functions through the dereferenced iterator.
— end note
 ]
Returns: f.
Complexity: Applies f exactly last - first times.
Remarks: If f returns a result, the result is ignored.
template<class ExecutionPolicy, class ForwardIterator, class Function> void for_each(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, Function f);
Preconditions: Function meets the Cpp17CopyConstructible requirements.
Effects: Applies f to the result of dereferencing every iterator in the range [first, last).
Note
:
If the type of first meets the requirements of a mutable iterator, f may apply non-constant functions through the dereferenced iterator.
— end note
 ]
Complexity: Applies f exactly last - first times.
Remarks: If f returns a result, the result is ignored.
Implementations do not have the freedom granted under [algorithms.parallel.exec] to make arbitrary copies of elements from the input sequence.
Note
:
Does not return a copy of its Function parameter, since parallelization may not permit efficient state accumulation.
— end note
 ]
template<input_­iterator I, sentinel_­for<I> S, class Proj = identity, indirectly_­unary_­invocable<projected<I, Proj>> Fun> constexpr ranges::for_each_result<I, Fun> ranges::for_each(I first, S last, Fun f, Proj proj = {}); template<input_­range R, class Proj = identity, indirectly_­unary_­invocable<projected<iterator_t<R>, Proj>> Fun> constexpr ranges::for_each_result<borrowed_iterator_t<R>, Fun> ranges::for_each(R&& r, Fun f, Proj proj = {});
Effects: Calls invoke(f, invoke(proj, *i)) for every iterator i in the range [first, last), starting from first and proceeding to last - 1.
Note
:
If the result of invoke(proj, *i) is a mutable reference, f may apply non-constant functions.
— end note
 ]
Returns: {last, std​::​move(f)}.
Complexity: Applies f and proj exactly last - first times.
Remarks: If f returns a result, the result is ignored.
Note
:
The overloads in namespace ranges require Fun to model copy_­constructible.
— end note
 ]
template<class InputIterator, class Size, class Function> constexpr InputIterator for_each_n(InputIterator first, Size n, Function f);
Mandates: The type Size is convertible to an integral type ([conv.integral], [class.conv]).
Preconditions: Function meets the Cpp17MoveConstructible requirements.
Note
:
Function need not meet the requirements of Cpp17CopyConstructible.
— end note
 ]
Preconditions: n >= 0 is true.
Effects: Applies f to the result of dereferencing every iterator in the range [first, first + n) in order.
Note
:
If the type of first meets the requirements of a mutable iterator, f may apply non-constant functions through the dereferenced iterator.
— end note
 ]
Returns: first + n.
Remarks: If f returns a result, the result is ignored.
template<class ExecutionPolicy, class ForwardIterator, class Size, class Function> ForwardIterator for_each_n(ExecutionPolicy&& exec, ForwardIterator first, Size n, Function f);
Mandates: The type Size is convertible to an integral type ([conv.integral], [class.conv]).
Preconditions: Function meets the Cpp17CopyConstructible requirements.
Preconditions: n >= 0 is true.
Effects: Applies f to the result of dereferencing every iterator in the range [first, first + n).
Note
:
If the type of first meets the requirements of a mutable iterator, f may apply non-constant functions through the dereferenced iterator.
— end note
 ]
Returns: first + n.
Remarks: If f returns a result, the result is ignored.
Implementations do not have the freedom granted under [algorithms.parallel.exec] to make arbitrary copies of elements from the input sequence.
template<input_iterator I, class Proj = identity, indirectly_­unary_­invocable<projected<I, Proj>> Fun> constexpr ranges::for_each_n_result<I, Fun> ranges::for_each_n(I first, iter_difference_t<I> n, Fun f, Proj proj = {});
Preconditions: n >= 0 is true.
Effects: Calls invoke(f, invoke(proj, *i)) for every iterator i in the range [first, first + n) in order.
Note
:
If the result of invoke(proj, *i) is a mutable reference, f may apply non-constant functions.
— end note
 ]
Returns: {first + n, std​::​move(f)}.
Remarks: If f returns a result, the result is ignored.
Note
:
The overload in namespace ranges requires Fun to model copy_­constructible.
— end note
 ]