SpeedPacer
SpeedPacer

Reputation: 55

Alternative to std::inner_product algorithm?

I know the std::inner_product algorithm will have problems if you're dealing with two arrays of different sizes. Is there another standard library algorithm that can work with arrays of different sizes, e.g. by automatically using the smaller of the two array sizes?

Upvotes: 2

Views: 499

Answers (1)

R Sahu
R Sahu

Reputation: 206607

Not too hard to implement one.

template <class InputIterator1, class InputIterator2, class T>
   T inner_product (InputIterator1 first1, InputIterator1 last1,
                    InputIterator2 first2, InputIterator2 last2, T init)
{
   for ( ; first1 != last1 && first2 != last2; ++first1, ++first2 )
   {
      init += (*first1)*(*first2);
   }
   return init;
}

Upvotes: 3

Related Questions