Questions tagged [accumulate]

Accumulation refers to the process of repeatedly combining the previously calculated result with the next item, until the supply of items is exhausted.

Accumulation refers to the process of repeatedly combining the previously calculated result with the next item, until the supply of items is exhausted.

When we are interested only in the final result, it is equivalent to left folding; and if we keep the whole sequence of results progressively calculated, it is known as left scan.

319 questions
132
votes
25 answers

How to find the cumulative sum of numbers in a list?

time_interval = [4, 6, 12] I want to sum up the numbers like [4, 4+6, 4+6+12] in order to get the list t = [4, 10, 22]. I tried the following: t1 = time_interval[0] t2 = time_interval[1] + t1 t3 = time_interval[2] + t2 print(t1, t2, t3) # -> 4 10…
user2259323
  • 1,339
  • 2
  • 9
  • 3
48
votes
5 answers

Understanding std::accumulate

I want to know why std::accumulate (aka reduce) 3rd parameter is needed. For those who do not know what accumulate is, it's used like so: vector V{1,2,3}; int sum = accumulate(V.begin(), V.end(), 0); // sum == 6 Call to accumulate is…
Leonid Volnitsky
  • 8,854
  • 5
  • 38
  • 53
26
votes
6 answers

How can I use std::accumulate and a lambda to calculate a mean?

I have a standard library container of large numbers, so large that they may cause overflow if I add them together. Let's pretend it's this container: std::vector v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; I want to calculate the mean of this…
EMBLEM
  • 2,207
  • 4
  • 24
  • 32
22
votes
2 answers

Is it possible to use std::accumulate with std::min?

I am trying to combine std::accumulate with std::min. Something like this (won't compile): vector V{2,1,3}; cout << accumulate(V.begin()+1, V.end(), V.front(), std::min); Is it possible? Is it possible to do without writing wrapper…
Leonid Volnitsky
  • 8,854
  • 5
  • 38
  • 53
20
votes
2 answers

Why has std::accumulate not been made constexpr in C++20?

In C++20, many (most?) C++-standard-library algorithms have been made constexpr. Yet - std::accumulate has not. It seems like it could have been: template constexpr T accumulate(InputIt first, InputIt last, T init) { for…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
20
votes
5 answers

Adding all values of map using std::accumulate

I am simply trying to add values of a map defined in the program below: std::map floor_plan; const size_t distance = std::accumulate(std::begin(floor_plan), std::end(floor_plan), 0); std::cout << "Total: " << distance; I get the…
Daqs
  • 720
  • 3
  • 8
  • 28
19
votes
4 answers

Why is the std::accumulate function showing the wrong sum of a vector?

Consider the following code for adding all the elements of a vector: #include #include #include #include using namespace std; int main(void) { std::vector V; V.push_back(1.2); …
smilingbuddha
  • 14,334
  • 33
  • 112
  • 189
13
votes
1 answer

summation of vector containing long long int using accumulation

#include #include #include #include using namespace std; int main() { int N; cin>>N; long long int x,sum=0; std::vector v; for(int i=0;i>x; …
Rakshit Verma
  • 131
  • 1
  • 4
10
votes
1 answer

How to choose between std::reduce and std::accumulate?

std::accumulate and std::reduce does almost the same thing. Summary of of std::reduce says it all: similar to `std::accumulate`, except out of order In many cases these functions should yield the same end result and exhibit the same overall…
darune
  • 10,480
  • 2
  • 24
  • 62
10
votes
1 answer

how does __glibcxx_function_requires and __glibcxx_requires_valid_range macros work?

template inline _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op) { // concept requirements …
Yoh0xFF
  • 1,450
  • 2
  • 18
  • 31
9
votes
2 answers

C++ vector accumulates

I was trying to use the accumulate function for vectors vector A; double B = 0; A.reserve(100); for(itr = 0; itr < 210; itr++) { term1 = pow(r[itr], 12); term1 = 1/term1; term2 = pow(r[itr], 6); term2 = 2/term2; …
Josh
  • 3,231
  • 8
  • 37
  • 58
9
votes
6 answers

Accumulate values for every possible combination in R

Let's say I have data test (dput given) where a list-col say items: test <- structure(list(items = list('a', c('b', 'c'), c('d', 'e'), 'f', c('g', 'h')), ID = c(1,1,1,2,2)), row.names = c(NA, 5L), class =…
AnilGoyal
  • 25,297
  • 4
  • 27
  • 45
9
votes
3 answers

Why does std::accumulate generate 705032704 as output instead of the sum of the elements in the vector?

The output is 705032704 instead of 5000000000. Why is that? I thought std::accumulate would compute the sum of the elements in the vector. #include #include #include #include #include #include…
Ruirui
  • 149
  • 7
9
votes
5 answers

Transform-and-Accumulate

Have anybody written a C++ STL-compliant algorithm that combines std::transform and std::accumulate into a single pass algorithm supporting both the unary, binary and perhaps even (n-ary!) variant, say std::transformed_accumulate? I want this…
Nordlöw
  • 11,838
  • 10
  • 52
  • 99
8
votes
2 answers

Accumulate absolute values of a vector

If I like to accumulate the absolute values of a std::vector, I can use a lambda to calculate the absolute value and add it to the sum of std::accumulate (live demo). #include #include int main (){ std::vector vec…
schorsch312
  • 5,553
  • 5
  • 28
  • 57
1
2 3
21 22