Forward Iterators in C++

In this tutorial, we will learn about forward iterators in C++, in detail.

Defining an iterator:

As we know, a container is a data structure capable of storing information of almost any type(excluding some restrictions).

Iterators, on the other hand, are objects similar to pointers. They are used to manipulate container elements.

Advantages of using an iterator:

Iterators have properties similar to those of pointers. This allows us to manipulate built-in arrays in the Standard Template Library using pointers as iterators. In many cases, many lines of code are reduced to a single statement due to the expressive power of iterators.

Types of iterators in C++:

There are five types of iterators in C++ based on their functionality:

  • input iterators
  • output iterators
  • forward iterators
  • bidirectional iterators
  • random-access iterators.

Hierarchy of iterators:

The hierarchy and interrelation between different iterators can be effectively described using the following diagram:

Forward Iterators in C++

Forward Iterators in C++:

Forward iterators combine the properties of both input and output iterators. Elements can be both accessed and modified using forward iterators. We should also note that bidirectional and random-access iterators are also valid forward iterators, except they possess some additional functionalities.

Iterator operations supported by forward iterators in C++:

  • pre-increment an iterator(++p)
  • post-increment an iterator(p++)
  • assign one iterator to another(p=p1)
  • dereference an iterator as an rvalue, or an lvalue(*p)
  • to read an element m(p->m)
  • compare iterators for equality(p==p1)
  • compare iterators for inequality(p!=p1)

Iterator operations not supported by forward iterators:

  • pre-decrement and post-decrementing an iterator(–p and p–)
  • comparing two iterators using <,<=,>,>=
  • returning a reference to the element using p[i] to measure the offset.
  • operations like p+=i, p-=i, p-p1.

Sample code to demonstrate the working of forward iterators in C++:

The code given below will help you understand forward iterators better:

#include<iostream>
#include<iterator>
#include<vector>
using namespace std;
template<class ForwardIterator>
void disp(ForwardIterator start, ForwardIterator last)
{
    while(start!=last)
       {
           cout<<*(start++)<<" ";
            start++;
       }

}
int main()
{
    vector<int> x;
    for(int i=1;i<=10;i++)
       {
        x.push_back(i);
       }
    disp(x.begin(),x.end());
    return 0;
}

The output for the above code is:

1 3 5 7 9
Process returned 0 (0x0)   execution time : 0.048 s
Press any key to continue.

On trying to perform any operations that are not supported by the forward iterator, an error message is displayed.

The main advantage of the forward iterator is that it is a multi-pass iterator, and its main disadvantage is that it allows traversal in only a single direction, that too, one step at a time.

With this, we come to the end of this tutorial.

Also, see:

Leave a Reply

Your email address will not be published.