InputRange
You’ll learn about the InputRange with the help of an example in this lesson.
We'll cover the following
Introduction
This type of range models the type of iteration where elements are accessed in sequence, as you have seen in the print()
functions above. Most algorithms only require that elements are iterated in the forward direction without needing to look at elements that have already been iterated over. InputRange also models the standard input streams of programs, where elements are removed from the stream as they are read.
For completeness, here are the three functions that InputRange requires:
-
empty
: specifies whether the range is empty; it must return true when the range is considered to be empty and false otherwise -
front
: provides access to the element at the beginning of the range -
popFront()
: shortens the range from the beginning by removing the first element
Note: We write empty and front without parentheses, as they can be seen as properties of the range, and we write
popFront()
with parentheses as it is a function with side effects.
Here is how print()
can be implemented by using these range functions:
Get hands-on with 1400+ tech skills courses.