Learn C++ STL

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

Module 11 · Iterators: The Thing Between a Container and an Algorithm

Every Iterator Operation, One by One, With Its Cost

Free with an accountReading

Bob keeps the club's waiting list in a std::list and wants the fourth name. He writes auto it = l.begin() + 3;, as he would on a vector, and GCC 12 answers with one error and a page of notes. Which operations does each iterator have, and what does each one cost? This lesson takes them one at a time.

Checking what this lesson needs for you…

What is inside Every Iterator Operation, One by One, With Its Cost

  1. begin and end, and std::begin, std::end and std::size
  2. cbegin and cend, and begin() on a const container
  3. rbegin, rend and base()
  4. * and ->
  5. ++it and it++
  6. --it, and the forward_list that refuses it
  7. Jumps: + n, - n, +=, [n], it2 - it1 and <
  8. std::advance, std::next and std::prev
  9. std::distance
  10. back_inserter: copying into a container that grows
  11. front_inserter and inserter
  12. istream_iterator: every number until the input ends
  13. The parentheses trapIntermediate
  14. ostream_iterator, and the separator after the last value
  15. When an iterator stops being validIntermediate
  16. iterator_traits and the five tags in codeIntermediate
  17. Every cost in one table