Use the generic count algorithm with predicate: Determine the number of array elements that are not equal to 1 : bind2nd « STL Algorithms Helper « C++






Use the generic count algorithm with predicate: Determine the number of array elements that are not equal to 1

  
 
#include <iostream>
#include <cassert>
#include <algorithm>
#include <functional>
using namespace std;

int main()
{
  int a[] = {0, 0, 0, 1, 1, 1, 2, 2, 2};

  int final_count = count_if(&a[0], &a[9], bind2nd(not_equal_to<int>(), 1));

  cout << final_count << endl;  
  return 0;
}

 /* 
6

 */       
    
  








Related examples in the same category

1.Demonstrate bind2nd()Demonstrate bind2nd()
2.transform, bind2nd and modulus
3.transform, bind2nd and plus
4.multiply by 100 to get a percent
5.Count vowels in a sentence