Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Added accumulate() method of vectors #674

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions snippets/vector/accumulate.cpp
Original file line number Diff line number Diff line change
@@ -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 <bits/stdc++.h>
#include <vector>
#include<numeric>

int main() {
std::vector<int> v = {3, 13, 27};
int sum = 7;
std::cout << "Result using accumulate: ";
std::cout << accumulate(v.begin(), v.end(), sum);

return 0;
}
24 changes: 24 additions & 0 deletions vector/accumulate.md
Original file line number Diff line number Diff line change
@@ -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 <bits/stdc++.h>
#include <vector>
#include<numeric>

int main() {
std::vector<int> 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)**