Junior task – sum of integers in vector Assume that we have vector of integers ie.

1 |
auto numbers = std::vector<int>{1,4,7,9,3,4,5}; |

and we want to write function which will sum up all its elements.

1 2 3 4 |
int sum(const std::vector<int>& numbers) { return std::accumulate(numbers.begin(), numbers.end(), 0); } |

In which cases it will work?

1 2 3 4 |
sum(numbers); // works - result 33 sum(std::vector<int>{1,4,7,9,3,4,5}); // works sum({1,4,7,9,3,4,5}); // works sum(std::vector<double>{1,4,7,9,3,4,5}); // don't work |

Middle task – sum of elements in vector no matter what type Let’s make it more generic… Read More