In this section, we concentrate on implementing an iterator that generates numbers from the Fibonacci number sequence while iterating over it.
- In order to be able to print the Fibonacci numbers to the terminal, we need to include a header first:
#include <iostream>
- We call the Fibonacci iterator, fibit. It will carry a member i, which saves the index position in the Fibonacci sequence, and a and b will be the variables that hold the last two Fibonacci values. If instantiated with the default constructor, a Fibonacci iterator will be initialized to the value F(0):
class fibit { size_t i {0}; size_t a {0}; size_t b {1};
- Next, we define the standard constructor and another constructor, which allows us to initialize the ...