Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ count number of element in vector

count(vect.begin(), vect.end(), 3)
Comment

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;
Comment

PREVIOUS NEXT
Code Example
Cpp :: substring to int c++ 
Cpp :: c++ string to int conversion 
Cpp :: C++ Find the sum of first n Natural Numbers 
Cpp :: c++ cin operator 
Cpp :: for in c++ 
Cpp :: define unicode c++ 
Cpp :: sleep system function linux c++ 
Cpp :: how to check if a number is prime c++ 
Cpp :: delete one specific character in string C++ 
Cpp :: how to change a value from an array c++ 
Cpp :: how to create a min priority queue of pair of int, int 
Cpp :: cpp float to string 
Cpp :: not in c++ 
Cpp :: initialize 2d vector 
Cpp :: setprecision c++ 
Cpp :: count bits c++ 
Cpp :: C++ break and continue 
Cpp :: pop_back 
Cpp :: sort index c++ 
Cpp :: indexing strings in c++ 
Cpp :: c++ public class syntax 
Cpp :: who to include a library c++ 
Cpp :: c++ rand include 
Cpp :: initialize dynamic array c++ to 0 
Cpp :: number of digits in int c++ 
Cpp :: Program To Calculate Number Power Using Recursion In C++. The power number should always be positive integer. 
Cpp :: gettimeofday header file 
Cpp :: Convert a hexadecimal number into decimal c++ 
Cpp :: position of max element in vector c++ 
Cpp :: c++ saying hello world 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =