 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count the number of 1’s and 0’s in a binary array using STL in C++
In this tutorial, we will be discussing a program to count the number of 1’s and 0’s in a binary array using STL in C++.
For this we will be provided with an array. Our task is to count the number of 0’s and 1’s present in the array.
Example
#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; }  Output
1's: 3 0's: 4
Advertisements
 