C++ Data StructuresLesson 55Free preview
C++ Iterators
This lesson is part of C++ · All levels
Summary
An iterator is a pointer object that runs along the container element, so the algorithm can work regardless of the type of container. A common iterator is provided in pairs begin() for the initial position and end() for the position immediately after the last element, and is accessed with the operator * or ->. Almost all STL algorithms accept iterators as a universal way of reaching data.
Key points
- Begin() points to the first element, end() points to the position after the last element.
- Reference with *it for value or it->member for struct/class member.
- The iteration stops when the iterator equals end().
- Vectors support random access iterators so they can be moved by + n.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.