Module 11 · Iterators: The Thing Between a Container and an Algorithm
Under the Hood: An Iterator Is a Wrapped Pointer
ProReading
Kenji printed sizeof for two iterators: a vector's and a list's. Both said 8 bytes. Yet the vector's iterator jumps a million places in under a microsecond, and the list's takes 10 to 14 milliseconds for the same jump. Maria asks the question this lesson answers: "But why, if they are the same size?" The answer is in what those 8 bytes hold, and GCC 12.2 lets us read it line by line.
Checking what this lesson needs for you…
What is inside Under the Hood: An Iterator Is a Wrapped Pointer
- Eight bytes each: what GCC 12 keeps in an iterator
- What ++ does: one add, one link, one climb, one hop
- reverse_iterator: it stores one place and reads the one before
- back_inserter: an assignment that calls push_back
- std::distance: a loop, a subtraction, or a stored size
- The performance lab: why a list walk is slow
- Watching an iterator go stale
- Debug mode: the library checks every iterator