How to accumulate vector elements in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <numeric>

using std::cout;
using std::endl;
using std::vector;

int main()
{
	vector<int> vec{ 1, 2, 3, 4, 5 };

	int total = accumulate(vec.begin(), vec.end(), 0);

	cout << total << endl;

	return 0;
}


/*
run:

15

*/

 



answered May 19, 2018 by avibootz
...