এই টিউটোরিয়ালে, আমরা C++ এ STL ব্যবহার করে বাইনারি অ্যারেতে 1 এবং 0 এর সংখ্যা গণনা করার জন্য একটি প্রোগ্রাম নিয়ে আলোচনা করব।
এর জন্য আমাদের একটি অ্যারে দেওয়া হবে। আমাদের কাজ হল অ্যারেতে উপস্থিত 0 এবং 1 এর সংখ্যা গণনা করা।
উদাহরণ
#include <bits/stdc++.h>
using namespace std;
// checking if element is 1 or not
bool isOne(int i){
if (i == 1)
return true;
else
return false;
}
int main(){
int a[] = { 1, 0, 0, 1, 0, 0, 1 };
int n = sizeof(a) / sizeof(a[0]);
int count_of_one = count_if(a, a + n, isOne);
cout << "1's: " << count_of_one << endl;
cout << "0's: " << (n - count_of_one) << endl;
return 0;
} আউটপুট
1's: 3 0's: 4