What iterator categories and concepts exist in modern C++, and how do their guarantees constrain algorithms?
Show answer and explanation
The classic hierarchy distinguishes input, forward, bidirectional, and random-access iterators, with output iterators describing writable traversal; C++20 also formalizes contiguous iterators and corresponding iterator concepts. Forward iterators add multipass behavior, bidirectional iterators add decrement, random-access iterators add constant-time jumps and ordering, and contiguous iterators additionally map to adjacent storage. An algorithm must request only the weakest operations it needs, because using stronger operations such as subtraction on a list iterator is ill-formed and can accidentally exclude valid ranges.
The classic hierarchy distinguishes input, forward, bidirectional, and random-access iterators, with output iterators describing writable traversal; C++20 also formalizes contiguous iterators and corresponding iterator concepts. Forward iterators add multipass behavior, bidirectional iterators add decrement, random-access iterators add constant-time jumps and ordering, and contiguous iterators additionally map to adjacent storage. An algorithm must request only the weakest operations it needs, because using stronger operations such as subtraction on a list iterator is ill-formed and can accidentally exclude valid ranges.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.