diff --git a/algorithm/inclusive_scan.md b/algorithm/inclusive_scan.md new file mode 100644 index 00000000..2bf55f95 --- /dev/null +++ b/algorithm/inclusive_scan.md @@ -0,0 +1,37 @@ +# inclusive_scan + +**Description :** Inclusive Scan computes a binary associative operation (`std::plus<>()` by default) on the elements from `first` (included) to `last` (excluded) and writes the results in the range starting from `d_first`; if provided an initial value will be used. Available in C++17 and above. + +**Example** : + +```cpp + // Compute prefix index sum of all the values in v + std::cout << "\n\nPrefix index sum of v: "; + std::inclusive_scan(v.begin(), + v.end(), + std::ostream_iterator(std::cout, " ")); + + // Compute prefix index sum of all the values in v with an initial value of 5 + std::cout << "\n\nPrefix index sum of v (with initial value 5): "; + std::inclusive_scan(v.begin(), + v.end(), + std::ostream_iterator(std::cout, " "), + std::plus<>{}, + 5); + + // Compute prefix index sum of the first 3 values in v + std::cout << "\n\nPrefix index sum for first 3 elements of v: "; + std::inclusive_scan(v.begin(), + v.begin() + 3, + std::ostream_iterator(std::cout, " ")); + + // Compute prefix index product of all the values in v + std::cout << "\n\nPrefix index product of v: "; + std::inclusive_scan(v.begin(), + v.end(), + std::ostream_iterator(std::cout, " "), + std::multiplies<>{}, + 1); +``` +**[See Sample code](../snippets/algorithm/inclusive_scan.cpp)**
+**[Run Code](https://rextester.com/UVR33146)** diff --git a/snippets/algorithm/inclusive_scan.cpp b/snippets/algorithm/inclusive_scan.cpp new file mode 100644 index 00000000..c00149e5 --- /dev/null +++ b/snippets/algorithm/inclusive_scan.cpp @@ -0,0 +1,51 @@ +/* + Author : Claudio Montanari + Date : 26/06/1995 + Time : 15:30 + Description : Computes inclusive prefix binary operation on a range (sum by default). +*/ + +#include +#include +#include +#include + +int main() { + std::vector v{ 1, 2, 3, 4, 3, 10 }; + + std::cout << "Content of v: "; + for (auto i : v) { + std::cout << i << " "; + } + + // Compute prefix index sum of all the values in v + std::cout << "\n\nPrefix index sum of v: "; + std::inclusive_scan(v.begin(), + v.end(), + std::ostream_iterator(std::cout, " ")); + + // Compute prefix index sum of all the values in v with an initial value of 5 + std::cout << "\n\nPrefix index sum of v (with initial value 5): "; + std::inclusive_scan(v.begin(), + v.end(), + std::ostream_iterator(std::cout, " "), + std::plus<>{}, + 5); + + // Compute prefix index sum of the first 3 values in v + std::cout << "\n\nPrefix index sum for first 3 elements of v: "; + std::inclusive_scan(v.begin(), + v.begin() + 3, + std::ostream_iterator(std::cout, " ")); + + // Compute prefix index product of all the values in v + std::cout << "\n\nPrefix index product of v: "; + std::inclusive_scan(v.begin(), + v.end(), + std::ostream_iterator(std::cout, " "), + std::multiplies<>{}, + 1); + + std::cout << std::endl; +} +