(C++) std::accumulate example 1: summing a std::vector of integers
January 7, 2018 ยท View on GitHub
std::accumulate example 1: summing a std::vector of integers is a std::accumulate example to sum a std::vector of integers.
#include <cassert>
#include <numeric>
#include <vector>
int main()
{
//Create a std::vector
std::vector<int> v;
for (int i=0; i!=10; ++i) { v.push_back(i); }
//Sum the std::vector
const int sum{std::accumulate(std::begin(v), std::end(v), 0)}; // '0' is the initial value
//Assume std::accumulate works correctly
assert(sum == 45);
}