Skip to content

Commit 5a9e635

Browse files
Add the transform examples for the std::reduce section.
1 parent abab8bb commit 5a9e635

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

CPP17.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,15 @@ std::reduce(std::cbegin(a), std::cend(a)); // == 6
560560
// Using a custom binary op:
561561
std::reduce(std::cbegin(a), std::cend(a), 1, std::multiplies<>{}); // == 6
562562
```
563+
Additionally you can specify transformations for reducers:
564+
```c++
565+
std::transform_reduce(std::cbegin(a), std::cend(a), 0, std::plus<>{}, times_ten); // == 60
566+
567+
const std::array<int, 3> b{ 1, 2, 3 };
568+
const auto product_times_ten = [](const auto a, const auto b) { return a * b * 10; };
569+
570+
std::transform_reduce(std::cbegin(a), std::cend(a), std::cbegin(b), 0, std::plus<>{}, product_times_ten); // == 140
571+
```
563572

564573
### Prefix sum algorithms
565574
Support for prefix sums (both inclusive and exclusive scans) along with transformations.

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,15 @@ std::reduce(std::cbegin(a), std::cend(a)); // == 6
12241224
// Using a custom binary op:
12251225
std::reduce(std::cbegin(a), std::cend(a), 1, std::multiplies<>{}); // == 6
12261226
```
1227+
Additionally you can specify transformations for reducers:
1228+
```c++
1229+
std::transform_reduce(std::cbegin(a), std::cend(a), 0, std::plus<>{}, times_ten); // == 60
1230+
1231+
const std::array<int, 3> b{ 1, 2, 3 };
1232+
const auto product_times_ten = [](const auto a, const auto b) { return a * b * 10; };
1233+
1234+
std::transform_reduce(std::cbegin(a), std::cend(a), std::cbegin(b), 0, std::plus<>{}, product_times_ten); // == 140
1235+
```
12271236

12281237
### Prefix sum algorithms
12291238
Support for prefix sums (both inclusive and exclusive scans) along with transformations.

0 commit comments

Comments
 (0)