Interactive Brokers Interview Question
template <typename Iterator, typename Function>
typename std::iterator_traits<Iterator>::value_type
Reduce(const Iterator begin, const Iterator end, Function fn)
throw (NotEnoughElements)
{
if (begin == end) throw NotEnoughElements();
Iterator it = begin;
typename std::iterator_traits<const Iterator>::value_type result = *it++;
if (it == end) throw NotEnoughElements();
for (; it != end; ++it) result = fn(result, *it);
return result;
}
- fiddler.g July 31, 2010