Iterators give you the ability to navigate from one item to another in a collection and track a specific position within a collection. This episode is part two and continues describing even more advanced iterator topics.
With both part 1 and part 2 of the iterator episodes, you’ve learned about the following iterator categories:
- Input Iterator – input iterators advance forward anytime you read from them and can only read each item once. You can compare an input iterator with another iterator that represents a end-of-input. This lets you know when you’ve read everything there is to read.
- Output Iterator – output iterators usually advance forward anytime you write a value to wherever the iterator is currently pointing to. You have no way to compare output iterators with another iterator. This means that you can’t compare an output iterator with an end or anything like that. All you can do is write and it’s up to you to figure out when to stop writing.
- Forward Iterator – forward iterators are like input iterators where you have control over when to advance to the next item. You can also write to some if not most forward iterators. And you can compare a forward iterator with another forward iterator and they’ll be equal if they both represent the same item.
- Bidirectional Iterator – bidirectional iterators are like forward iterators with the additional ability to advance forward or move backward. This gives you the ability to backtrack and process items as many times as you need.
- Random-Access Iterator – random-access iterators provide all the capabilities of bidirectional iterators plus the ability to move forward or backward to any item. This means they support pointer arithmetic. Random-access iterators give you the ability to calculate differences between iterators and determine if one iterator is less than or greater than another.
And in addition to the iterator categories, you also now know about iterator adapters. this episode discusses the following iterator adapters:
- Reverse Iterator – reverse iterators are usually available from collections that support at least bidirectional iterators. The reverse iterators start at the end of the collection and advance backwards.
- Move Iterator – move iterators convert an iterator so that the items it refers to can be moved to another collection instead of being copied.
- Insert Iterator – insert iterators come in three varieties, back inserters, front inserters, and general inserters. Insert iterators don’t really advance at all. They just keep adding new items to a collection anytime you write to the iterator.
- Stream Iterator – stream iterators can wrap up input or output streams so you can work with the information in a stream as if it was items in a collection.