c 2b 2b count vector elements

Solutions on MaxInterview for c 2b 2b count vector elements by the best coders in the world

showing results for - "c 2b 2b count vector elements"
Dillon
08 Nov 2020
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;