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

@@ -64,6 +64,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++14 includes the following new language features:
- [binary literals](#binary-literals)
@@ -1224,6 +1225,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
```
## C++14 Language Features
### Binary literals