Create an Iterable Range

Learn to create an iterable range.

We'll cover the following

This recipe describes a simple class that generates an iterable range, suitable for use with the range-based for loop. The idea is to create a sequence generator that iterates from a beginning value to an ending value.

To accomplish this task, we need an iterator class, along with the object interface class.

How to do it

There's two major parts to this recipe, the main interface, Seq, and the iterator class.

  • First, we'll define the Seq class. It only needs to implement the begin() and end() member functions:

Get hands-on with 1400+ tech skills courses.