C++C++11C++14C++17IteratorsLearn C++

Introduction to C++ Iterators

Iterator abstracts the concept of pointer to the sequence in range [begin, end), and thus provides the following basic operations:

  • dereference to obtain the current element;
  • movement to point the next element;
  • comparison to determine at least the end of sequence.

Iterator to the element of the sequence in range [begin, end) is valid from the moment of initialization and until:

  • the moment it goes out of the scope;
  • the moment of destruction or modifying (such as resizing) the underlying storage (usually, a container where the sequence is actually stored).

Please note, that the sequence range of [begin, end) is half-opened, since the end is serves as the element one-past-the-last element of the sequence. An attempt of reading or writing by using the end may lead to the undefined behavior of the program, and therefore illegal. Iterator that points to the end is primarily used to determine the end of sequence by using the comparison operator !=. Also, the end is conventionally used by functions as the return value to indicate the search failure (i.e. “the requested element not found” condition), for example:

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

Related posts
C++C++11C++14C++17C++20

What Is The Stack (std::stack) In Modern C++?

C++C++11C++14C++17C++20Learn C++

What Is The Queue (std::queue) In Modern C++?

C++C++11C++14C++17Learn C++SyntaxTemplates

What Are The Logical Operation Metafunctions In Modern C++?

C++C++14C++17C++20Learn C++

What Are The Deprecated C++14 Features In C++17?

Worth reading...
Categories of Iterators in C++