Using accumulate to calculate alternate sum

  accumulate, c++

If I use the accumulate function in C++ like the below

 std::vector<int> v2{1, 2, 3, 4, 5};
 int sum = 0;
 std::cout << std::accumulate(v2.begin(), v2.end())

It will simply sum all the numbers. But I wanted to calculate 1-2+3-4+5
I was thinking of some way to accumulate 1,3,5 and 2,4 separately and then do it
But not sure how to achieve this using accumulate.

Source: Windows Questions C++

LEAVE A COMMENT