5.2.21 · D1C++ Programming

Foundations — Iterators — input, output, forward, bidirectional, random access, contiguous

2,159 words10 min readBack to topic

Before you can understand the six iterator categories in the parent note, you must be fluent in a handful of tiny symbols. This page builds every one of them from nothing. We go slowly and in an order where each idea leans only on the ones above it.


0. A collection and its elements

Start with the most concrete picture: a row of boxes, each holding a value.

Figure — Iterators — input, output, forward, bidirectional, random access, contiguous

We are NOT yet saying how the boxes are laid out in memory. That difference (all-in-a-line vs. scattered) is exactly what will separate the categories later. For now: boxes, values, done.


1. The finger: what it means

Figure — Iterators — input, output, forward, bidirectional, random access, contiguous

The finger is a separate object from the box. You can have two fingers on the same row, move one, and the other stays put — that idea (multi-pass) becomes important later.


2. *it — the star means "the value under the finger"


3. ++it — the plus-plus means "step to the next box"

Figure — Iterators — input, output, forward, bidirectional, random access, contiguous

--it (in the figure, the gray arrow going left) is the mirror move: one box to the left. Notice it is drawn faded — not every finger is allowed to go backward. That restriction is exactly what will define bidirectional vs forward later.


4. begin, end, and the sentinel


5. == and != — comparing two fingers

Note this compares positions, never values. Two fingers on two different boxes that happen to hold the same number are not equal.


6. it + n, it - n, it[n] — jumping many boxes at once

Figure — Iterators — input, output, forward, bidirectional, random access, contiguous

7. <, > — ordering two fingers


8. Contiguous memory: &*(it+n) == &*it + n

This is the densest symbol in the parent, so we unpack it letter by letter.

Now read &*(it + n) right-to-left, one symbol at a time:

  1. it + n — finger boxes right (needs random access, §6).
  2. *(...) — the value under that finger (§2).
  3. &(...) — the address of that value.

So &*(it+n) is "the house number of the box steps along". The claim &*(it+n) == &*it + n says:

Figure — Iterators — input, output, forward, bidirectional, random access, contiguous

9. Subset and the refinement chain

This is why the parent's operation table never removes a ✓ as you move right: once a power appears, all richer categories inherit it.


10. The tag types (std::..._iterator_tag)


How the foundations feed the topic

Container = row of boxes

Iterator it = finger on one box

star it = read or write the value

plus plus it = step forward

begin end and the stop post

equals and not equals compare positions

it plus n = jump many boxes fast

less than orders fingers

address of gives contiguous line

Refinement chain of six categories

Category tags select fast code at compile time


Equipment checklist

Cover the right side and answer aloud. If any answer is fuzzy, re-read that section before the parent note.

What does it (an iterator) represent in one word?
A finger pointing at one element of a collection.
What is the difference between it and *it?
it is the position (the finger); *it is the value under it.
What does ++it change — the boxes or the finger?
Only the finger; it slides one box to the right.
Where does end() point, and why not at the last element?
One box past the last, so loops can stop cleanly with it != end.
Does it1 == it2 compare positions or values?
Positions — same box or not, regardless of value.
What extra promise does it + n make beyond pressing ++it n times?
It reaches the target in one constant-time leap, no matter how big n is.
Read &*(it + n) right-to-left in plain words.
Address-of the value under the finger that is n boxes to the right.
What does A ⊂ B mean for categories?
Every capability of A is kept by B, and B adds more (refinement).
Why can a deque do it + n but fail the contiguous test?
Its boxes live in separate chunks, so addresses don't rise in one even line.
What is a category tag and when is it read?
An empty label-struct naming the category, read at compile time to pick the fastest code.