Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) ProductNonZeroPositives

 

Math code snippet to calculate the product of all positive values in a std::vector.

 

//From http://www.richelbilderbeek.nl/CppAccumulate_if.htm
template
  <
  typename InputIterator,
  typename ElementType,
  typename BinaryOperation,
  typename Predicate
  >
const ElementType std::accumulate_if(
  InputIterator first,
  const InputIterator last,
  ElementType init,
  const BinaryOperation binary_op,
  const Predicate predicate)
{
  for (; first != last; ++first)
    if (predicate(*first)) init = binary_op(init, *first);
  return init;
}

#include <functional>
#include <vector>

//From http://www.richelbilderbeek.nl/CppProductNonZeroPositives.htm
int ProductNonZeroPositives(const std::vector<int>& v)
{
  return ::std::accumulate_if(
    v.begin(),
    v.end(),
    1,
    std::multiplies<int>(),
    std::bind2nd(std::greater<int>(),0));
}

 

 

 

 

 

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

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict