visual studio 2010 - Check number of zeros elements 1D vector in C++ -


i using 1d vector in c++ such as

std::vector<int> a; 

have way check or count number of zeros element in using c++. example

   a[0]=3;    a[1]=0;    a[2]=2;    a[3]=0; 

then number of zeros element in 2.

a simple iteration on vector, increasing count everytime 0 element encountered seems fine.

int count = 0; (int = 0; < a.size(); i++)   {     if (a[i] == 0)       count++;   } return count; 

you can use count function.

int ans = count(a.begin(), a.end(), 0); 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -