Consume InputIterator with C++ ranges

  Kiến thức lập trình

With an input iterator, I can consume different sub-ranges out of it, e.g.

void test(std::input_iterator auto it) {
  while (*it < 1) ++it; // drop_while
  int n = *it++; // take(1)
  int sum = 0; while (n-- > 0) sum += (1 + *it++); // fold_left(take(n) | transform)
  int prd = 1; while (int x = *it++ / 2) prd *= x; // fold_left(transform | take_while)
  std::cout << sum << ' ' << prd << 'n';
}

int main() {
    test(std::begin({0, 0, 0, 3, 400, 30, 2, 4, 6, 0}));
}

Is there a way to do the same with std::ranges/std::views?

LEAVE A COMMENT