Files
cppdraft_translate/cppdraft/alg/foreach.md
2025-10-25 03:02:53 +03:00

17 KiB
Raw Blame History

[alg.foreach]

26 Algorithms library [algorithms]

26.6 Non-modifying sequence operations [alg.nonmodifying]

26.6.5 For each [alg.foreach]

🔗

template<class InputIterator, class Function> constexpr Function for_each(InputIterator first, InputIterator last, Function f);

1

#

Preconditions: Function meets the Cpp17MoveConstructible requirements (Table 31).

[Note 1:

Function need not meet the requirements ofCpp17CopyConstructible (Table 32).

— end note]

2

#

Effects: Applies f to the result of dereferencing every iterator in the range [first, last), starting from first and proceeding to last - 1.

[Note 2:

If the type of first meets the requirements of a mutable iterator,f can apply non-constant functions through the dereferenced iterator.

— end note]

3

#

Returns: f.

4

#

Complexity: Applies f exactly last - first times.

5

#

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);

6

#

Preconditions: Function meets the Cpp17CopyConstructible requirements.

7

#

Effects: Applies f to the result of dereferencing every iterator in the range [first, last).

[Note 3:

If the type of first meets the requirements of a mutable iterator,f can apply non-constant functions through the dereferenced iterator.

— end note]

8

#

Complexity: Applies f exactly last - first times.

9

#

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.

10

#

[Note 4:

Does not return a copy of its Function parameter, since parallelization often does not permit efficient state accumulation.

— end note]

🔗

template<[input_iterator](iterator.concept.input#concept:input_iterator "24.3.4.9Concept input_­iterator[iterator.concept.input]") I, [sentinel_for](iterator.concept.sentinel#concept:sentinel_for "24.3.4.7Concept sentinel_­for[iterator.concept.sentinel]")<I> S, class Proj = identity, [indirectly_unary_invocable](indirectcallable.indirectinvocable#concept:indirectly_unary_invocable "24.3.6.3Indirect callables[indirectcallable.indirectinvocable]")<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](range.refinements#concept:input_range "25.4.6Other range refinements[range.refinements]") R, class Proj = identity, [indirectly_unary_invocable](indirectcallable.indirectinvocable#concept:indirectly_unary_invocable "24.3.6.3Indirect callables[indirectcallable.indirectinvocable]")<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 = {});

11

#

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 5:

If the result of invoke(proj, *i) is a mutable reference,f can apply non-constant functions.

— end note]

12

#

Returns: {last, std::move(f)}.

13

#

Complexity: Applies f and proj exactly last - first times.

14

#

Remarks: If f returns a result, the result is ignored.

15

#

[Note 6:

The overloads in namespace ranges requireFun to model copy_constructible.

— end note]

🔗

template<[execution-policy](algorithms.parallel.defns#concept:execution-policy "26.3.1Preamble[algorithms.parallel.defns]") Ep, [random_access_iterator](iterator.concept.random.access#concept:random_access_iterator "24.3.4.13Concept random_­access_­iterator[iterator.concept.random.access]") I, [sized_sentinel_for](iterator.concept.sizedsentinel#concept:sized_sentinel_for "24.3.4.8Concept sized_­sentinel_­for[iterator.concept.sizedsentinel]")<I> S, class Proj = identity, [indirectly_unary_invocable](indirectcallable.indirectinvocable#concept:indirectly_unary_invocable "24.3.6.3Indirect callables[indirectcallable.indirectinvocable]")<projected<I, Proj>> Fun> I ranges::for_each(Ep&& exec, I first, S last, Fun f, Proj proj = {}); template<[execution-policy](algorithms.parallel.defns#concept:execution-policy "26.3.1Preamble[algorithms.parallel.defns]") Ep, [sized-random-access-range](range.refinements#concept:sized-random-access-range "25.4.6Other range refinements[range.refinements]") R, class Proj = identity, [indirectly_unary_invocable](indirectcallable.indirectinvocable#concept:indirectly_unary_invocable "24.3.6.3Indirect callables[indirectcallable.indirectinvocable]")<projected<iterator_t<R>, Proj>> Fun> borrowed_iterator_t<R> ranges::for_each(Ep&& exec, R&& r, Fun f, Proj proj = {});

16

#

Effects: Calls invoke(f, invoke(proj, *i)) for every iterator i in the range [first, last).

[Note 7:

If the result of invoke(proj, *i) is a mutable reference,f can apply non-constant functions.

— end note]

17

#

Returns: last.

18

#

Complexity: Applies f and proj exactly last - first times.

19

#

Remarks:

[Note 8:

Does not return a copy of its Fun parameter, since parallelization often does not permit efficient state accumulation.

— end note]

🔗

template<class InputIterator, class Size, class Function> constexpr InputIterator for_each_n(InputIterator first, Size n, Function f);

20

#

Mandates: The type Size is convertible to an integral type ([conv.integral], [class.conv]).

21

#

Preconditions: n >= 0 is true.

Function meets the Cpp17MoveConstructible requirements.

[Note 9:

Function need not meet the requirements of Cpp17CopyConstructible.

— end note]

22

#

Effects: Applies f to the result of dereferencing every iterator in the range [first, first + n) in order.

[Note 10:

If the type of first meets the requirements of a mutable iterator,f can apply non-constant functions through the dereferenced iterator.

— end note]

23

#

Returns: first + n.

24

#

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);

25

#

Mandates: The type Size is convertible to an integral type ([conv.integral], [class.conv]).

26

#

Preconditions: n >= 0 is true.

Function meets the Cpp17CopyConstructible requirements.

27

#

Effects: Applies f to the result of dereferencing every iterator in the range [first, first + n).

[Note 11:

If the type of first meets the requirements of a mutable iterator,f can apply non-constant functions through the dereferenced iterator.

— end note]

28

#

Returns: first + n.

29

#

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](iterator.concept.input#concept:input_iterator "24.3.4.9Concept input_­iterator[iterator.concept.input]") I, class Proj = identity, [indirectly_unary_invocable](indirectcallable.indirectinvocable#concept:indirectly_unary_invocable "24.3.6.3Indirect callables[indirectcallable.indirectinvocable]")<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 = {});

30

#

Preconditions: n >= 0 is true.

31

#

Effects: Calls invoke(f, invoke(proj, *i)) for every iterator i in the range [first, first + n) in order.

[Note 12:

If the result of invoke(proj, *i) is a mutable reference,f can apply non-constant functions.

— end note]

32

#

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

33

#

Remarks: If f returns a result, the result is ignored.

34

#

[Note 13:

The overload in namespace ranges requires Fun to model copy_constructible.

— end note]

🔗

template<[execution-policy](algorithms.parallel.defns#concept:execution-policy "26.3.1Preamble[algorithms.parallel.defns]") Ep, [random_access_iterator](iterator.concept.random.access#concept:random_access_iterator "24.3.4.13Concept random_­access_­iterator[iterator.concept.random.access]") I, class Proj = identity, [indirectly_unary_invocable](indirectcallable.indirectinvocable#concept:indirectly_unary_invocable "24.3.6.3Indirect callables[indirectcallable.indirectinvocable]")<projected<I, Proj>> Fun> I ranges::for_each_n(Ep&& exec, I first, iter_difference_t<I> n, Fun f, Proj proj = {});

35

#

Preconditions: n >= 0 is true.

36

#

Effects: Calls invoke(f, invoke(proj, *i)) for every iterator i in the range [first, first + n).

[Note 14:

If the result of invoke(proj, *i) is a mutable reference,f can apply non-constant functions.

— end note]

37

#

Returns: first + n.

38

#

Remarks:

[Note 15:

Does not return a copy of its Fun parameter, since parallelization often does not permit efficient state accumulation.

— end note]