mirror of
https://github.com/AnthonyCalandra/modern-cpp-features.git
synced 2025-12-17 10:04:35 +03:00
Add the prefix sum algorithms.
This commit is contained in:
21
CPP17.md
21
CPP17.md
@@ -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.
|
||||
|
||||
21
README.md
21
README.md
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user