Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

c++ count vector elements

// 1.
for(std::vector<int>::iterator it = vector.begin(); it != vector.end(); ++it)
    sum_of_elems += *it;

// 2.
// #include <numeric>
sum_of_elems = std::accumulate(vector.begin(), vector.end(), 0);

// C++11 and higher
// 3.
// #include <numeric>
sum_of_elems = std::accumulate(vector.begin(), vector.end(),
                               decltype(vector)::value_type(0));

// 4.
for (auto& n : vector)
    sum_of_elems += n;
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #count #vector #elements
ADD COMMENT
Topic
Name
7+1 =