(C++) CountNonZeroPositives
January 7, 2018 · View on GitHub
(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); }