Extending the IntIterator to bidirectional

In order to make it possible to iterate a range of numbers in reverse, we add the operator--() method to IntIterator and upgradeiterator_category to std::bidirectional_iterator_tag:

class IntIterator {  ...  using iterator_category = std::bidirectional_iterator_tag;  ...  auto& operator--() { --value_; return *this; }  ...};

This is an example of iterating in reverse order:

for(auto it = IntIterator{12}; it != IntIterator{-1}; --it) {  std::cout << *it << " ";}// Prints: 12 11 10 9 8 7 6 5 4 3 2 1 0

Get C++ High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.