1// 1.
2for(std::vector<int>::iterator it = vector.begin(); it != vector.end(); ++it)
3 sum_of_elems += *it;
4
5// 2.
6// #include <numeric>
7sum_of_elems = std::accumulate(vector.begin(), vector.end(), 0);
8
9// C++11 and higher
10// 3.
11// #include <numeric>
12sum_of_elems = std::accumulate(vector.begin(), vector.end(),
13 decltype(vector)::value_type(0));
14
15// 4.
16for (auto& n : vector)
17 sum_of_elems += n;