46: Collections: Iterators Part 2.
Take Up Code - En podcast af Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Kategorier:
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. Listen to the full episode or read the full transcript below. Transcript The first thing to understand is that iterators are a concept. They’re an idea and because of this, anything that behaves like an iterator is an iterator. The previous episode mentioned how you can work with raw pointers in a basic array as if they are iterators. Pointers are iterators because they can be advanced forward or backward and know how to move the appropriate number of bytes. They can move from one item to the next without you needing to worry about how they do this. And they remember what item they currently point to. That’s all it takes for anything to be an iterator. This means that you can create iterators that look like they follow these rules but actually do very different things. These are still iterators and this is why I decided to talk about these in a second episode. Be ready to think about things in a very different way. Here we go. The first topic I’ll explain is called reverse iterators. How is a reverse iterator any different than just going backward through a bidirectional iterator? Well going backward means that you ha