C++ Forward Iterator - C++ Programming Tutorial
C++ Course / Miscellaneous / C++ Forward Iterator

C++ Forward Iterator

BLUF: Mastering C++ Forward Iterator is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: C++ Forward Iterator

C++ is renowned for its efficiency. Learn how C++ Forward Iterator enables low-level control and high-performance computing in the tutorial below.

  • Forward Iterator is a combination of Bidirectional and Random Access iterator. Therefore, we can say that the forward iterator can be used to read and write to a container.
  • Forward iterators are used to read the contents from the beginning to the end of a container.
  • Forward iterator use only increments operator (++) to move through all the elements of a container. Therefore, we can say that the forward iterator can only move forward.
  • A Forward iterator is a multi-pass iterator.
  • Operations Performed on the Forward Iterator:

Properties Valid Expressions
It is default constructible. A x;
It is a copy-constructible. A x(y);
It is a copy-assignable. y = x;
It can be compared either by using an equality or inequality operator. a==b;a!=b;
It can be incremented. a++;++a;
It can be dereferenced as an rvalue. *a;
It can also be dereferenced as an lvalue. *a = t;

Where 'A' is a forward iterator type, and x and y are the objects of a forward iterator type, and t is an objeccpp tutorialed by the iterator type object.

Let's see a simple example:

Example

#include <iostream>

#include <fstream>

#include <iterator>

#include <vector>

using namespace std;

template<class ForwardIterator>                                       // function template

void display(ForwardIterator first, ForwardIterator last)            // display function

{

 while(first!=last)

 {

     cout<<*first<<" ";

     first++;

 }

}

int main()

{

  vector<int> a;                          // declaration of vector.

  for(int i=1;i<=10;i++)

  {

      a.push_back(i);

  }

  display(a.begin(),a.end());                // calling display() function.

  return 0;

}

Output:

Output

1 2 3 4 5 6 7 8 9 10

Features of the Forward Iterator:

A forward iterator can be compared using either equality or inequality operators to determine if they are equal or not.

Suppose 'A' and 'B' are the two iterators:

Example

A==B;           // equality operator

A!=B;            // inequality operator
  • Dereferencing : Forward iterators allow us to dereference them both as rvalues and lvalues. This means we can retrieve the value from the output iterator and also update it with a new value.

Suppose we have an iterator 'A' and an integer variable 't':

A forward iterator can be advanced but does not support moving backward.

Suppose 'A' is an iterator:

Example

A++;

++A;

A decrementable iterator is unable to be decremented since it exclusively progresses in the forward direction.

Suppose 'A' is an iterator:

Example

A--;          // invalid
  • Relational Operators : Equality operator can be used with a forward iterator, while other relational operators cannot be utilized with it.

Suppose 'A' and 'B' are the two iterators:

Example

A==B;        // valid

A>=B;        // invalid
  • Arithmetic Operators : Arithmetic operators are not compatible with the forward iterator.

A forward iterator lacks the capability of randomly accessing elements, restricting it to sequential iteration through a container's elements.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience