Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) CountNonZeroPositives

 

Code snippet to count the number of non-zero positive values in a std::vector.

 

#include <algorithm
#include <functional>
#include <vector>

//From http://www.richelbilderbeek.nl/CppCountNonZeroPositives
int CountNonZeroPositives(const std::vector<int>& v)
{
 return std::count_if(
    v.begin(),
    v.end(),
    std::bind2nd(std::greater<int>(),0));
}

 

 

 

 

 

CountNonZeroPositives test

 

#include <cassert>

int main()
{
  std::vector<int> v;
  v.push_back(-3); assert(CountNonZeroPositives(v)==0);
  v.push_back(-2); assert(CountNonZeroPositives(v)==0);
  v.push_back(-1); assert(CountNonZeroPositives(v)==0);
  v.push_back( 0); assert(CountNonZeroPositives(v)==0);
  v.push_back( 1); assert(CountNonZeroPositives(v)==1);
  v.push_back( 2); assert(CountNonZeroPositives(v)==2);
  v.push_back( 3); assert(CountNonZeroPositives(v)==3);
  v.push_back( 4); assert(CountNonZeroPositives(v)==4);
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict