Home >>C++ Standard Template Library Tutorial(STL) >C++ Forward Iterator

C++ Forward Iterator

C++ Forward Iterator

  • Forward Iterator in C++ can be generally used to read and write to a container and hence it can be said that the forward iterator is basically a combination of Forward and Random Access iterator.
  • In order to read the contents from the beginning to the end of a container the forward iterators are generally used.
  • Only increments operator (++) is used by the forward iterator in order to move through all the elements of a container. Hence, it can be concluded that the forward iterator can only move forward.
  • A Forward iterator in C++ is also known as a multi-pass iterator.

Operations Performed on the Forward Iterator

Here are the list of the operations that are generally performed on the forward iterators:

Property Expressions
The forward iterator is generally default constructible. A x;
The forward iterator is generally copy-constructible. A x(y);
The forward iterator is generally copy-assignable. y = x;
The forward iterator can generally be compared either by using an equality or inequality operator. a++;
++a;
Forward iterator can generally be incremented. a++;
++a;
Forward iterator can generally be dereferenced as an lvalue. *a = t;
Forward iterator can generally be dereferenced as an rvalue. *a;

In the above mentioned table, 'A' is a forward iterator type, and x and y are the objects that belongs to the forward iterator type, and t is generally an object that has been pointed by the iterator type object.

Features of the Forward Iterator

Here are some of the features of the forward iterators:

  • Equality/Inequality operator : Users can compare the forward iterator just by using an equality or inequality operator.
  • Dereferencing : For both these values like as an lvalue and rvalue the forward iterator can be generally dereferenced and the programmers can assign the value to the output iterator and assign a value to it.
  • Incrementable : Incrimination of the forward iterator can be done but it cannot be decremented.

Limitations Of Forward Iterator

Here are the limitations that generally apply to the Forward Iterator:

  • Relational operator : With the forward iterator users can generally use an equality but the condition is that the other iterators will not be applied on the forward iterator in C++.
  • Arithmetic operator : This type of operator cannot be applied to the forward iterator.
  • Decrementable : Decrementation is not possible in the forward iterator as it moves only in the forward direction.
  • Random Access : The random access of an element is not possible in the forward iterator as this can only iterate through the elements of a container.

Example of Forward Iterator

Here is an example that will give you a deep understanding of the subject:

#include <iostream>  
#include <fstream>  
#include <iterator>  
#include <vector>  

using namespace std;  
  
template<class ForwardIterator>                               
void show(ForwardIterator start, ForwardIterator end)   
{  
	 while(start!=end)  
	 {  
		 cout<<*start<<" ";  
		 start++;  
	 }  
}  
  
int main()  
{  
  
  vector<int> x;  
  for(int i=1;i<=10;i++)  
  {  
      x.push_back(i);  
  }  
  show(x.begin(),x.end());  
  
  return 0;  
}
Output :
1 2 3 4 5 6 7 8 9 10

No Sidebar ads