1. Problem It Solves
Iterator categories describe supported movement and access, while invalidation rules say when container changes make stored iterators unusable. It makes an important constraint visible instead of leaving readers to guess. This lesson keeps only the C++11 core that fits one focused day.
2. Prerequisites
The ideas from Day 39, plus basic variables, functions, and output already introduced.
3. Core Idea
Mental model: Iterator categories describe supported movement and access, while invalidation rules say when container changes make stored iterators unusable. Identify the relevant value or state, who owns it, and whether the rule acts during compilation or execution.
4. Minimal Syntax
auto it = v.begin(); v.reserve(3); // capacity decisions affect validity5. How It Works
The example creates a tiny fixed state with no keyboard input.
C++11 or the standard-library contract applies today's rule.
The program prints the important result so it can be checked against the source.
6. Common Mistakes
Dereferencing an invalidated iterator is undefined behavior even if the old address appears to contain the expected value.
7. When to Use It
Use it when choosing algorithms and preserving traversal state across container changes.
Avoid it when it hides ownership, lifetime, type, ordering, or cost.
8. Simple Example
A reserved vector keeps an iterator valid for one insertion, while a list iterator is advanced with category-neutral std::advance. The .cpp keeps the data fixed and avoids unrelated abstraction.
Complete sample code
Source file
cpp11/40_iterator_categories_invalidation/main.cpp
#include <iostream>
#include <iterator>
#include <list>
#include <vector>
int main() {
std::vector<int> numbers;
numbers.reserve(3);
numbers.push_back(10);
numbers.push_back(20);
std::vector<int>::iterator first = numbers.begin();
numbers.push_back(30); // no reallocation: reserved capacity is 3
std::cout << "vector_first=" << *first << '\n';
std::list<int> linked{4, 5, 6};
std::list<int>::iterator middle = linked.begin();
std::advance(middle, 1); // works for non-random-access iterators
std::cout << "list_middle=" << *middle << '\n';
// Another vector insertion may reallocate and invalidate first.
}
9. Key Takeaways
The feature is part of the C++11 scope used in this course.
Understand its lifetime, ownership, type, and ordering consequences.
Compile with warnings and prefer the smallest form that makes the rule obvious.
10. Self-Check Questions
Easy — What different properties do iterator category and iterator invalidation describe? Why must old iterators be reconsidered after a
std::vectorreallocates?Medium — Read the small example described above. What value or state should it print, and which rule produces that result?
Hard — Find and explain the subtle bug in this situation: Dereferencing an invalidated iterator is undefined behavior even if the old address appears to contain the expected value. What is the smallest C++11-safe correction?