diff --git a/snippets/vector/accumulate.cpp b/snippets/vector/accumulate.cpp new file mode 100644 index 00000000..870d74a5 --- /dev/null +++ b/snippets/vector/accumulate.cpp @@ -0,0 +1,20 @@ +// Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide +/* + Author : Deepanshu Midha + Date : 01/10/2022 + Time : 05:50 + Description : Returns the sum of all the values lying in a range between [first, last) +*/ + +#include +#include +#include + +int main() { + std::vector v = {3, 13, 27}; + int sum = 7; + std::cout << "Result using accumulate: "; + std::cout << accumulate(v.begin(), v.end(), sum); + + return 0; +} diff --git a/vector/accumulate.md b/vector/accumulate.md new file mode 100644 index 00000000..fa130225 --- /dev/null +++ b/vector/accumulate.md @@ -0,0 +1,24 @@ +# Accumulate + +**Description :** +- This function returns the sum of all the values lying in a range between [first, last) with the variable sum. +- Usually the sum of elements in a particular range or a complete array is found using a linear operation which requires adding all the elements in the range one by one and storing it into some variable after each iteration. + +**Example** : + +```cpp +#include +#include +#include + +int main() { + std::vector v = {3, 13, 27}; + int sum = 7; + std::cout << "Result using accumulate: "; + std::cout << accumulate(v.begin(), v.end(), sum); + + return 0; +} +``` +**[See Sample code](https://github.com/deepanshumidha5140/30-seconds-of-cpp/blob/master/snippets/vector/accumulate.cpp)** +**[Run Code](https://rextester.com/BFYS36316)**