forked from Bhupesh-V/30-seconds-of-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Bhupesh-V#524 from claudioMontanari/feature/inclus…
…ive_scan Feature/inclusive scan
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<int>(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<int>(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<int>(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<int>(std::cout, " "), | ||
std::multiplies<>{}, | ||
1); | ||
``` | ||
**[See Sample code](../snippets/algorithm/inclusive_scan.cpp)**<br> | ||
**[Run Code](https://rextester.com/UVR33146)** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <iostream> | ||
#include <vector> | ||
#include <numeric> | ||
#include <iterator> | ||
|
||
int main() { | ||
std::vector<int> 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<int>(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<int>(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<int>(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<int>(std::cout, " "), | ||
std::multiplies<>{}, | ||
1); | ||
|
||
std::cout << std::endl; | ||
} | ||
|