The ranges library offers a facility called views. Views allow the construction of immutable and cheap data ranges from iterators. They don't copy the data—they just refer to it. We can use view to filter all of the even numbers from our collection:
ranges::view::filter(numbers, isEven)
Views can be composed without any copying and by using the composition operator, |. For example, we can obtain the list of numbers divisible by 6 by composing two filters: the first one on even numbers, and the second on numbers divisible by 3. Given a new predicate that checks whether a number is multiple of 3, we use the following:
auto isMultipleOf3 = [](const auto number){
return number % 3 == 0;
};
We obtain the list of numbers divisible by 6 through the following composition:
numbers | ranges::view::filter(isEven) | ranges::view::filter...