//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));
}
|