| 10 | // which only works for random-access iterators) |
| 11 | template <typename T, typename IterType> |
| 12 | std::optional<T> average(IterType begin, IterType end) |
| 13 | { |
| 14 | const auto count{ std::distance(begin, end) }; |
| 15 | const auto sum{ std::accumulate(begin, end, T{}) }; |
| 16 | |
| 17 | return count ? std::optional<T>{ sum / count } : std::nullopt; |
| 18 | } |
| 19 | |
| 20 | // Solution 2: accumulate a pair<> that counts both the number of elements and the sum |
| 21 | //template <typename T, typename IterType> |
nothing calls this directly
no outgoing calls
no test coverage detected