Menu
Coddy logo textTech

Forward Iterators

Lesson 16 of 23 in Coddy's C++ - Standard Template Library course.

Forward Iterator is a combination of input and output iterator, so we can say that the forward iterator is an iterator that can be used to read and write to a container. This leads to being able to access, as well as modify values with the iterator. 

As we can see, the bidirectional iterator and random access iterator are also valid forward iterators.


So, we will learn about the features of a forward iterator.


  • Equality/Inequality operator
    A forward iterator can be compared by using equality or an inequality operator.
A == B // Equality
A != B // Inequality

  • Dereferencing
    We can dereference the forward iterator to use the two functionalities, accessing the value of the element that the iterator is pointing to, as well as assigning value to the element that the iterator is pointing to.
*A = X;
X = *A;

  • Incrementable
    A forward iterator can be incremented exactly like the input and output iterator, but cannot be decremented;
A++; // Post increment operator
++A; // Pre increment operator
A--; or --A; // Wrong

  • Swappable
    The value of one forward iterator can be exchanged or swapped with another.

A conclusion is that the forward iterator is an iterator which represents a sum of the features the input and output iterator have.

 

Try it yourself

This lesson doesn't include a code challenge.

All lessons in C++ - Standard Template Library