I am using 1D vector in C++ such as
std::vector<int> A;
Have any way to check or count the number of zeros element in A using C++. For example
A[0]=3;
A[1]=0;
A[2]=2;
A[3]=0;
Then number of zeros element in A is 2.
A simple iteration over the vector, increasing the count everytime a zero element is encountered seems fine.
int count = 0;
for (int i = 0; i < A.size(); i++)
{
if (A[i] == 0)
count++;
}
return count;
You can also use the count
function.
int ans = count(A.begin(), A.end(), 0);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.