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

436 lines
17 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[alg.foreach]
# 26 Algorithms library [[algorithms]](./#algorithms)
## 26.6 Non-modifying sequence operations [[alg.nonmodifying]](alg.nonmodifying#alg.foreach)
### 26.6.5 For each [alg.foreach]
[🔗](#lib:for_each)
`template<class InputIterator, class Function>
constexpr Function for_each(InputIterator first, InputIterator last, Function f);
`
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4616)
*Preconditions*: Function meets
the [*Cpp17MoveConstructible*](utility.arg.requirements#:Cpp17MoveConstructible "16.4.4.2Template argument requirements[utility.arg.requirements]") requirements (Table [31](utility.arg.requirements#tab:cpp17.moveconstructible "Table 31: Cpp17MoveConstructible requirements"))[.](#1.sentence-1)
[*Note [1](#note-1)*:
Function need not meet the requirements of[*Cpp17CopyConstructible*](utility.arg.requirements#:Cpp17CopyConstructible "16.4.4.2Template argument requirements[utility.arg.requirements]") (Table [32](utility.arg.requirements#tab:cpp17.copyconstructible "Table 32: Cpp17CopyConstructible requirements (in addition to Cpp17MoveConstructible)"))[.](#1.sentence-2)
— *end note*]
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4625)
*Effects*: Applies f to the result of dereferencing
every iterator in the range [first, last),
starting from first and proceeding to last - 1[.](#2.sentence-1)
[*Note [2](#note-2)*:
If the type of first meets the requirements of a mutable iterator,f can apply non-constant functions through the dereferenced iterator[.](#2.sentence-2)
— *end note*]
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4635)
*Returns*: f[.](#3.sentence-1)
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4639)
*Complexity*: Applies f exactly last - first times[.](#4.sentence-1)
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4643)
*Remarks*: If f returns a result, the result is ignored[.](#5.sentence-1)
[🔗](#lib:for_each_)
`template<class ExecutionPolicy, class ForwardIterator, class Function>
void for_each(ExecutionPolicy&& exec,
ForwardIterator first, ForwardIterator last,
Function f);
`
[6](#6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4657)
*Preconditions*: Function meets the [*Cpp17CopyConstructible*](utility.arg.requirements#:Cpp17CopyConstructible "16.4.4.2Template argument requirements[utility.arg.requirements]") requirements[.](#6.sentence-1)
[7](#7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4661)
*Effects*: Applies f to the result of dereferencing
every iterator in the range [first, last)[.](#7.sentence-1)
[*Note [3](#note-3)*:
If the type of first meets the requirements of a mutable iterator,f can apply non-constant functions through the dereferenced iterator[.](#7.sentence-2)
— *end note*]
[8](#8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4670)
*Complexity*: Applies f exactly last - first times[.](#8.sentence-1)
[9](#9)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4674)
*Remarks*: If f returns a result, the result is ignored[.](#9.sentence-1)
Implementations do not have
the freedom granted under [[algorithms.parallel.exec]](algorithms.parallel.exec "26.3.3Effect of execution policies on algorithm execution") to make arbitrary copies of elements from the input sequence[.](#9.sentence-2)
[10](#10)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4681)
[*Note [4](#note-4)*:
Does not return a copy of its Function parameter,
since parallelization often does not permit efficient state accumulation[.](#10.sentence-1)
— *end note*]
[🔗](#lib:for_each__)
`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](#11)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4701)
*Effects*: Calls invoke(f, invoke(proj, *i)) for every iterator i in the range [first, last),
starting from first and proceeding to last - 1[.](#11.sentence-1)
[*Note [5](#note-5)*:
If the result of invoke(proj, *i) is a mutable reference,f can apply non-constant functions[.](#11.sentence-2)
— *end note*]
[12](#12)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4711)
*Returns*: {last, std::move(f)}[.](#12.sentence-1)
[13](#13)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4715)
*Complexity*: Applies f and proj exactly last - first times[.](#13.sentence-1)
[14](#14)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4719)
*Remarks*: If f returns a result, the result is ignored[.](#14.sentence-1)
[15](#15)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4723)
[*Note [6](#note-6)*:
The overloads in namespace ranges requireFun to model [copy_constructible](concept.copyconstructible#concept:copy_constructible "18.4.14Concept copy_­constructible[concept.copyconstructible]")[.](#15.sentence-1)
— *end note*]
[🔗](#itemdecl:4)
`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](#16)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4742)
*Effects*: Calls invoke(f, invoke(proj, *i)) for every iterator i in the range [first, last)[.](#16.sentence-1)
[*Note [7](#note-7)*:
If the result of invoke(proj, *i) is a mutable reference,f can apply non-constant functions[.](#16.sentence-2)
— *end note*]
[17](#17)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4752)
*Returns*: last[.](#17.sentence-1)
[18](#18)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4756)
*Complexity*: Applies f and proj exactly last - first times[.](#18.sentence-1)
[19](#19)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4760)
*Remarks*:
- [(19.1)](#19.1)
If f returns a result, the result is ignored[.](#19.1.sentence-1)
- [(19.2)](#19.2)
Implementations do not have the freedom granted under [[algorithms.parallel.exec]](algorithms.parallel.exec "26.3.3Effect of execution policies on algorithm execution") to make arbitrary copies of elements from the input sequence[.](#19.2.sentence-1)
- [(19.3)](#19.3)
f may modify objects via its arguments ([[algorithms.parallel.user]](algorithms.parallel.user "26.3.2Requirements on user-provided function objects"))[.](#19.3.sentence-1)
[*Note [8](#note-8)*:
Does not return a copy of its Fun parameter,
since parallelization often does not permit
efficient state accumulation[.](#19.sentence-2)
— *end note*]
[🔗](#lib:for_each_n)
`template<class InputIterator, class Size, class Function>
constexpr InputIterator for_each_n(InputIterator first, Size n, Function f);
`
[20](#20)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4785)
*Mandates*: The type Size is convertible
to an integral type ([[conv.integral]](conv.integral "7.3.9Integral conversions"), [[class.conv]](class.conv "11.4.8Conversions"))[.](#20.sentence-1)
[21](#21)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4790)
*Preconditions*: n >= 0 is true[.](#21.sentence-1)
Function meets the [*Cpp17MoveConstructible*](utility.arg.requirements#:Cpp17MoveConstructible "16.4.4.2Template argument requirements[utility.arg.requirements]") requirements[.](#21.sentence-2)
[*Note [9](#note-9)*:
Function need not meet
the requirements of [*Cpp17CopyConstructible*](utility.arg.requirements#:Cpp17CopyConstructible "16.4.4.2Template argument requirements[utility.arg.requirements]")[.](#21.sentence-3)
— *end note*]
[22](#22)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4799)
*Effects*: Applies f to the result of dereferencing
every iterator in the range [first, first + n) in order[.](#22.sentence-1)
[*Note [10](#note-10)*:
If the type of first meets the requirements of a mutable iterator,f can apply non-constant functions through the dereferenced iterator[.](#22.sentence-2)
— *end note*]
[23](#23)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4808)
*Returns*: first + n[.](#23.sentence-1)
[24](#24)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4812)
*Remarks*: If f returns a result, the result is ignored[.](#24.sentence-1)
[🔗](#lib:for_each_n_)
`template<class ExecutionPolicy, class ForwardIterator, class Size, class Function>
ForwardIterator for_each_n(ExecutionPolicy&& exec, ForwardIterator first, Size n,
Function f);
`
[25](#25)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4825)
*Mandates*: The type Size is convertible
to an integral type ([[conv.integral]](conv.integral "7.3.9Integral conversions"), [[class.conv]](class.conv "11.4.8Conversions"))[.](#25.sentence-1)
[26](#26)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4830)
*Preconditions*: n >= 0 is true[.](#26.sentence-1)
Function meets the [*Cpp17CopyConstructible*](utility.arg.requirements#:Cpp17CopyConstructible "16.4.4.2Template argument requirements[utility.arg.requirements]") requirements[.](#26.sentence-2)
[27](#27)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4835)
*Effects*: Applies f to the result of dereferencing
every iterator in the range [first, first + n)[.](#27.sentence-1)
[*Note [11](#note-11)*:
If the type of first meets the requirements of a mutable iterator,f can apply non-constant functions through the dereferenced iterator[.](#27.sentence-2)
— *end note*]
[28](#28)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4844)
*Returns*: first + n[.](#28.sentence-1)
[29](#29)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4848)
*Remarks*: If f returns a result, the result is ignored[.](#29.sentence-1)
Implementations do not have
the freedom granted under [[algorithms.parallel.exec]](algorithms.parallel.exec "26.3.3Effect of execution policies on algorithm execution") to make arbitrary copies of elements from the input sequence[.](#29.sentence-2)
[🔗](#lib:for_each_n__)
`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](#30)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4865)
*Preconditions*: n >= 0 is true[.](#30.sentence-1)
[31](#31)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4869)
*Effects*: Calls invoke(f, invoke(proj, *i)) for every iterator i in the range
[first, first + n) in order[.](#31.sentence-1)
[*Note [12](#note-12)*:
If the result of invoke(proj, *i) is a mutable reference,f can apply non-constant functions[.](#31.sentence-2)
— *end note*]
[32](#32)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4879)
*Returns*: {first + n, std::move(f)}[.](#32.sentence-1)
[33](#33)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4883)
*Remarks*: If f returns a result, the result is ignored[.](#33.sentence-1)
[34](#34)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4887)
[*Note [13](#note-13)*:
The overload in namespace ranges requires Fun to model [copy_constructible](concept.copyconstructible#concept:copy_constructible "18.4.14Concept copy_­constructible[concept.copyconstructible]")[.](#34.sentence-1)
— *end note*]
[🔗](#itemdecl:8)
`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](#35)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4901)
*Preconditions*: n >= 0 is true[.](#35.sentence-1)
[36](#36)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4905)
*Effects*: Calls invoke(f, invoke(proj, *i)) for every iterator i in the range [first, first + n)[.](#36.sentence-1)
[*Note [14](#note-14)*:
If the result of invoke(proj, *i) is a mutable reference,f can apply non-constant functions[.](#36.sentence-2)
— *end note*]
[37](#37)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4915)
*Returns*: first + n[.](#37.sentence-1)
[38](#38)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L4919)
*Remarks*:
- [(38.1)](#38.1)
If f returns a result, the result is ignored[.](#38.1.sentence-1)
- [(38.2)](#38.2)
Implementations do not have the freedom granted under [[algorithms.parallel.exec]](algorithms.parallel.exec "26.3.3Effect of execution policies on algorithm execution") to make arbitrary copies of elements from the input sequence[.](#38.2.sentence-1)
- [(38.3)](#38.3)
f may modify objects via its arguments ([[algorithms.parallel.user]](algorithms.parallel.user "26.3.2Requirements on user-provided function objects"))[.](#38.3.sentence-1)
[*Note [15](#note-15)*:
Does not return a copy of its Fun parameter,
since parallelization often does not permit
efficient state accumulation[.](#38.sentence-2)
— *end note*]