Step 6 of 6
std::span
std::span<T> is to arrays what string_view is to strings: a non-owning view of contiguous elements. One function can then accept a C array, a std::array or a std::vector:
#include <span>
int sum(std::span<const int> xs) {
int s = 0;
for (int x : xs) s += x;
return s;
}
int a[] = {1, 2, 3};
std::vector<int> v = {4, 5};
sum(a); sum(v); sum(std::span(v).subspan(1));
Your turn: write int max_window(std::span<const int> xs, std::size_t k) that returns the largest sum of k consecutive elements. Use subspan or a sliding window. Assume 1 <= k <= xs.size().