×

C++ Forward Iterators

Iterators :

Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and assign values, and apply various operators to them in order to get the required outcome.

Forward Iterators :

Both of the input and output iterators are served by forward iterators. As a result, these iterators are called input-output operators. The values may be accessed (input iterator’s functionality) as well as assigned (functionality of output iterators). In one-way iterators, as their name implies, only forward movement is allowed. Additionally, Bidirectional and Random access iterators are valid forward iterators. In C++, the Forward Iterator can be used to read and write to a container, hence it can be claimed that the forward iterator is essentially a mix of the Forward and Random Access iterators. Forward iterators are commonly used to read the contents of a container from the start to end.

The forward iterator uses just the increment operator (++) to traverse all of the items of a container. In C++, a forward iterator is sometimes referred to as a multi-pass iterator.

Operations on Forward Iterators :

The following is a list of operations that are commonly performed on forward iterators :

  • The forward iterator is usually constructible by default.

Expression :

A i;
  • In most cases, the forward iterator can be copied.

Expression :

A i(j);
  • In most cases, the forward iterator can be copied.

Expression :

j = i;
  • In general, an equality or inequality operator can be used to compare the forward iterator.

Expression :

x++;
++x
  • In general, the forward iterator can be increased.

Expression :

x++;
++x
  • In most cases, a forward iterator may be dereferenced as a lvalue.

Expression :

*x=t;
  • In most cases, a forward iterator can be dereferenced to a rvalue.

Expression :

*x;

In the demonstration above, 'A' represents a forward iterator type, i and j represent forward iterator type objects, and t represents an object that has been directed by the iterator type object.

Features of the Forward Iterators :

The following are some of the characteristics of forward iterators:

Equality/Inequality operator :

The forward iterator may be compared simply by utilizing an equality or inequality operator.

Dereferencing :

The forward iterator may be dereferenced for both these values, such as a lvalue and a rvalue, and coders can allocate the value to the output iterator and give a value to it.

Incrementable :

The forward iterator can be incremented, but it can't be decremented.

Example of Forward Iterator in C++ :

#include <iostream>
#include <vector>
using namespace std;
int main()
{	// Declaring a vector vect1
	vector<int> vect1 = { 10, 20, 30, 40, 50 };


	// Declaring the iterator itr1
	vector<int>::iterator itr1;


	for (itr1 = vect1.begin(); itr1 != vect1.end(); ++itr1) {
		// Assigning the values to the locations that are pointed by the iterator
		*itr1 = 10;
	}


	for (itr1 = vect1.begin(); itr1 != vect1.end(); ++itr1) {
		// Accessing the values at the locations that are pointed by the iterator
		cout << (*itr1) << " ";
	}


	return 0;
}

Output :

10 10 10 10 10

Explanation :

In the above example, we declared a vector list called vect1 and an iterator called itr1. Then we assigned the values to the locations that were pointed by the iterator pointer. After that, we accessed the values at the locations that were pointed by the iterator pointer. As we can see, the iterator may be accessed as well as assigned a value, indicating that it is at least a forward iterator which can be any element in the hierarchy.

Drawbacks of Forward Iterator :

The following are the limits that are applicable to the Forward Iterator in general:

  • Relational operator :
    Users can utilize an equality with the forward iterator in most cases, but the constraint is that the other iterators would not be applied to the forward iterator in C++.
  • Arithmetic operator:
    The forward iterator does not support this sort of operator.
  • Decrementable :
    Because the forward iterator only advances forward, it is not feasible to increment it.
  • Random Access :
    The forward iterator can only iterate across the items in a container, therefore random access to an element is not feasible.
  • Unidirectional :
    These iterators are unidirectional, as previously stated. They can only move ahead, that is, they can only be increased. You can't take anything away from them.
  • Offset Dereference Operator ([]) :
    The forward iterators do not support the offset dereference operator ([]). As a result, the offset operator cannot be used to dereference a forward iterator.

Related Topics

User-defined literals in C++

Introduction to User-Defined Literals: User-defined literals, introduced in C++11, are a way to extend the C++ language to allow users to define their literal suffixes and the corresponding behavior. These suffixes...

7 minutes read.

Factory Method for Designing Pattern in C++

In C++, the factory method is a type of conditional design pattern. The factory method is related to creating a new object in C++. With the help of a factory...

3 minutes read.

C++ Overriding

C++ Function Overriding When the base and derive class both contain the same function name and calling the function through derived object invokes derived class function called function overriding. Function overloading is...

1 minute read.

Bit Manipulation in C++

The high-level language in which we communicate is not understood by the computer. As a result, there existed a standard mechanism for understanding any instruction sent to the computer. At...

5 minutes read.

C++ Convert Int to String

In fact, the conversion of numbers to strings or vice versa represents a significant paradigm change. We often need to convert a number to a string or a string to...

2 minutes read.

C++ Program to Print Fibonacci Triangle

Fibonacci Triangle Program in CPP Definition: Fibonacci Triangle as the name suggests is the same as the Fibonacci number series where the next element is the sum of the previous two elements....

3 minutes read.

Free vs delete() in C++

Free vs delete() in C++ In this section, we will learn about the free() function and also create a C ++ program of the delete operator. What is free() Function in C++? In...

4 minutes read.

Bitwise Operator vs Logical Operator

Bitwise Operator  Bitwise operators perform operations bit by bit on bits.The value is converted to abinary during operations like addition, subtraction, division, and so on. These operations are carried out at the...

3 minutes read.

What are local class and global class in C++

In C++, a class is a fundamental block that achieves object-oriented programming. The class holds its data members and member functions. Objects help to access the data elements and functions....

4 minutes read.

C++ array to function

Arrays in C++ : Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. An array can be declared by specifying the variable...

4 minutes read.

C++ Break

In this article, we will discuss the C++ Break statement with its syntax, algorithm, pseudocode, and examples. The C++ break statement also terminates the currently active loop or switch statement immediately....

4 minutes read.

How to declare a 2D array dynamically in C++

In this article, we will learn how to declare the dynamic array in C++. We also learn the initialization of a 2D array using a pointer in C++. Here, we...

3 minutes read.

Sliding Window Technique in C++

Sliding Window Technique or Window Sliding Technique is a computational technique that is mainly used to reduce the use of nested loop and replace it with a single loop. It...

4 minutes read.

C++ Type Casting

The type casting of variables in the C++ programming language will be covered in this section. The term "type casting" describes how software converts one data type to another. There...

9 minutes read.

C++ Heap Sort

Heapsort is executed on the structure of the heap data. We know heap is a complete tree in binary form. The heap tree can be of two different types: Min-heap,...

3 minutes read.

Observer Design Pattern in C++

The Observer design pattern is a behavioural design pattern that allows an object (known as the subject) to notify other objects (known as observers) when its state changes. This is...

3 minutes read.

Include Guards in C++

In C++ programming, we frequently utilize a class more than once, so it is necessary to create a header file and include it in the main program. Now, occasionally a...

3 minutes read.

Constructor Overloading

The program contains more than one constructor in a class with the same name, and different types of arguments are called constructor overloading. Calling of constructor depends on the number and types...

2 minutes read.

Scope Resolution Operator vs this Pointer in C++

In this tutorial, we will compare the Scope Resolution operation to this Pointer in C++ language. Scope Resolution Operator The Scope Resolution Operator in C++ programming language is usually denoted by (::)....

3 minutes read.

Maps in C++

Maps: Maps in C++ are the containers associated with key and mapped values. By keys and mapped values, we mean that the maps are used to store elements formed by the...

4 minutes read.