Add the prefix sum algorithms.

This commit is contained in:
Anthony Calandra
2023-02-20 19:23:51 -05:00
parent 3ade18fb41
commit abab8bb1a3
2 changed files with 42 additions and 0 deletions

View File

@@ -34,6 +34,7 @@ C++17 includes the following new library features:
- [std::sample](#stdsample)
- [std::clamp](#stdclamp)
- [std::reduce](#stdreduce)
- [prefix sum algorithms](#prefix-sum-algorithms)
## C++17 Language Features
@@ -560,6 +561,26 @@ std::reduce(std::cbegin(a), std::cend(a)); // == 6
std::reduce(std::cbegin(a), std::cend(a), 1, std::multiplies<>{}); // == 6
```
### Prefix sum algorithms
Support for prefix sums (both inclusive and exclusive scans) along with transformations.
```c++
const std::array<int, 3> a{ 1, 2, 3 };
std::inclusive_scan(std::cbegin(a), std::cend(a),
std::ostream_iterator<int>{ std::cout, " " }, std::plus<>{}); // 1 3 6
std::exclusive_scan(std::cbegin(a), std::cend(a),
std::ostream_iterator<int>{ std::cout, " " }, 0, std::plus<>{}); // 0 1 3
const auto times_ten = [](const auto n) { return n * 10; };
std::transform_inclusive_scan(std::cbegin(a), std::cend(a),
std::ostream_iterator<int>{ std::cout, " " }, std::plus<>{}, times_ten); // 10 30 60
std::transform_exclusive_scan(std::cbegin(a), std::cend(a),
std::ostream_iterator<int>{ std::cout, " " }, 0, std::plus<>{}, times_ten); // 0 10 30
```
## Acknowledgements
* [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features.
* [C++ Rvalue References Explained](http://thbecker.net/articles/rvalue_references/section_01.html) - a great introduction I used to understand rvalue references, perfect forwarding, and move semantics.