mirror of
https://github.com/AnthonyCalandra/modern-cpp-features.git
synced 2025-12-17 01:54:36 +03:00
Add the transform examples for the std::reduce section.
This commit is contained in:
9
CPP17.md
9
CPP17.md
@@ -560,6 +560,15 @@ std::reduce(std::cbegin(a), std::cend(a)); // == 6
|
|||||||
// Using a custom binary op:
|
// Using a custom binary op:
|
||||||
std::reduce(std::cbegin(a), std::cend(a), 1, std::multiplies<>{}); // == 6
|
std::reduce(std::cbegin(a), std::cend(a), 1, std::multiplies<>{}); // == 6
|
||||||
```
|
```
|
||||||
|
Additionally you can specify transformations for reducers:
|
||||||
|
```c++
|
||||||
|
std::transform_reduce(std::cbegin(a), std::cend(a), 0, std::plus<>{}, times_ten); // == 60
|
||||||
|
|
||||||
|
const std::array<int, 3> b{ 1, 2, 3 };
|
||||||
|
const auto product_times_ten = [](const auto a, const auto b) { return a * b * 10; };
|
||||||
|
|
||||||
|
std::transform_reduce(std::cbegin(a), std::cend(a), std::cbegin(b), 0, std::plus<>{}, product_times_ten); // == 140
|
||||||
|
```
|
||||||
|
|
||||||
### Prefix sum algorithms
|
### Prefix sum algorithms
|
||||||
Support for prefix sums (both inclusive and exclusive scans) along with transformations.
|
Support for prefix sums (both inclusive and exclusive scans) along with transformations.
|
||||||
|
|||||||
@@ -1224,6 +1224,15 @@ std::reduce(std::cbegin(a), std::cend(a)); // == 6
|
|||||||
// Using a custom binary op:
|
// Using a custom binary op:
|
||||||
std::reduce(std::cbegin(a), std::cend(a), 1, std::multiplies<>{}); // == 6
|
std::reduce(std::cbegin(a), std::cend(a), 1, std::multiplies<>{}); // == 6
|
||||||
```
|
```
|
||||||
|
Additionally you can specify transformations for reducers:
|
||||||
|
```c++
|
||||||
|
std::transform_reduce(std::cbegin(a), std::cend(a), 0, std::plus<>{}, times_ten); // == 60
|
||||||
|
|
||||||
|
const std::array<int, 3> b{ 1, 2, 3 };
|
||||||
|
const auto product_times_ten = [](const auto a, const auto b) { return a * b * 10; };
|
||||||
|
|
||||||
|
std::transform_reduce(std::cbegin(a), std::cend(a), std::cbegin(b), 0, std::plus<>{}, product_times_ten); // == 140
|
||||||
|
```
|
||||||
|
|
||||||
### Prefix sum algorithms
|
### Prefix sum algorithms
|
||||||
Support for prefix sums (both inclusive and exclusive scans) along with transformations.
|
Support for prefix sums (both inclusive and exclusive scans) along with transformations.
|
||||||
|
|||||||
Reference in New Issue
Block a user