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

46 lines
2.5 KiB
Markdown
Raw Permalink 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.

[accumulate]
# 26 Algorithms library [[algorithms]](./#algorithms)
## 26.10 Generalized numeric operations [[numeric.ops]](numeric.ops#accumulate)
### 26.10.3 Accumulate [accumulate]
[🔗](#lib:accumulate)
`template<class InputIterator, class T>
constexpr T accumulate(InputIterator first, InputIterator last, T init);
template<class InputIterator, class T, class BinaryOperation>
constexpr T accumulate(InputIterator first, InputIterator last, T init,
BinaryOperation binary_op);
`
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L12302)
*Preconditions*: T meets
the [*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)"))
and [*Cpp17CopyAssignable*](utility.arg.requirements#:Cpp17CopyAssignable "16.4.4.2Template argument requirements[utility.arg.requirements]") (Table [34](utility.arg.requirements#tab:cpp17.copyassignable "Table 34: Cpp17CopyAssignable requirements (in addition to Cpp17MoveAssignable)")) requirements[.](#1.sentence-1)
In the range [first, last],binary_op neither modifies elements
nor invalidates iterators or subranges[.](#1.sentence-2)[205](#footnote-205 "The use of fully closed ranges is intentional.")
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L12314)
*Effects*: Computes its result by
initializing the accumulator acc with the initial value init and then modifies it withacc = std::move(acc) + *i oracc = binary_op(std::move(acc), *i) for every iterator i in the range [first, last) in order[.](#2.sentence-1)[206](#footnote-206 "accumulate is similar to the APL reduction operator and Common Lisp reduce function, but it avoids the difficulty of defining the result of reduction on an empty sequence by always requiring an initial value.")
[205)](#footnote-205)[205)](#footnoteref-205)
The use of fully closed ranges is intentional[.](#footnote-205.sentence-1)
[206)](#footnote-206)[206)](#footnoteref-206)
accumulate is similar
to the APL reduction operator and Common Lisp reduce function,
but it avoids the difficulty of defining the result of reduction
on an empty sequence by always requiring an initial value[.](#footnote-206.sentence-1)