From 5a9e63549ee2d755c0061927b36ab61737aec012 Mon Sep 17 00:00:00 2001 From: Anthony Calandra Date: Mon, 20 Feb 2023 19:57:45 -0500 Subject: [PATCH] Add the transform examples for the std::reduce section. --- CPP17.md | 9 +++++++++ README.md | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/CPP17.md b/CPP17.md index a4b2492..07e3189 100644 --- a/CPP17.md +++ b/CPP17.md @@ -560,6 +560,15 @@ std::reduce(std::cbegin(a), std::cend(a)); // == 6 // Using a custom binary op: 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 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 Support for prefix sums (both inclusive and exclusive scans) along with transformations. diff --git a/README.md b/README.md index db13925..90f33cc 100644 --- a/README.md +++ b/README.md @@ -1224,6 +1224,15 @@ std::reduce(std::cbegin(a), std::cend(a)); // == 6 // Using a custom binary op: 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 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 Support for prefix sums (both inclusive and exclusive scans) along with transformations.