Learn C++ STL

Lesson 5 of 9 · Iterators: The Thing Between a Container and an Algorithm

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

  1. Eight bytes each: what GCC 12 keeps in an iterator
  2. What ++ does: one add, one link, one climb, one hop
  3. reverse_iterator: it stores one place and reads the one before
  4. back_inserter: an assignment that calls push_back
  5. std::distance: a loop, a subtraction, or a stored size
  6. The performance lab: why a list walk is slow
  7. Watching an iterator go stale
  8. Debug mode: the library checks every iterator