Javatpoint Logo
Javatpoint Logo

C++ Iterators

Iterators are just like pointers used to access the container elements.

Important Points:

  • Iterators are used to traverse from one element to another element, a process is known as iterating through the container.
  • The main advantage of an iterator is to provide a common interface for all the containers type.
  • Iterators make the algorithm independent of the type of the container used.
  • Iterators provide a generic approach to navigate through the elements of a container.

Syntax

Operations Performed on the Iterators:

  • Operator (*) : The '*' operator returns the element of the current position pointed by the iterator.
  • Operator (++) : The '++' operator increments the iterator by one. Therefore, an iterator points to the next element of the container.
  • Operator (==) and Operator (!=) : Both these operators determine whether the two iterators point to the same position or not.
  • Operator (=) : The '=' operator assigns the iterator.

Difference b/w Iterators & Pointers

Iterators can be smart pointers which allow to iterate over the complex data structures. A Container provides its iterator type. Therefore, we can say that the iterators have the common interface with different container type.

The container classes provide two basic member functions that allow to iterate or move through the elements of a container:

  • begin(): The begin() function returns an iterator pointing to the first element of the container.
  • end(): The end() function returns an iterator pointing to the past-the-last element of the container.
C++ Iterators

Let's see a simple example:

Output:

1 2 3 4 5

Iterator Categories

An iterator can be categorized in the following ways:

  • Input Iterator
  • Output Iterator
  • Forward Iterator
  • Bidirectional Iterator
  • Random Access Iterator
C++ Iterators

Input Iterator: An input iterator is an iterator used to access the elements from the container, but it does not modify the value of a container.

Operators used for an input iterator are:

  • Increment operator(++)
  • Equal operator(==)
  • Not equal operator(!=)
  • Dereference operator(*)

Output Iterator: An output iterator is an iterator used to modify the value of a container, but it does not read the value from a container. Therefore, we can say that an output iterator is a write-only iterator.

Operators used for an output iterator are:

  • Increment operator(++)
  • Assignment operator(=)

Forward Iterator: A forward iterator is an iterator used to read and write to a container. It is a multi-pass iterator.

Operators used for a Forward iterator are:

  • Increment operator(++)
  • Assignment operator(=)
  • Equal operator(=)
  • Not equal operator(!=)

Bidirectional iterator: A bidirectional iterator is an iterator supports all the features of a forward iterator plus it adds one more feature, i.e., decrement operator(--). We can move backward by decrementing an iterator.

Operators used for a Bidirectional iterator are:

  • Increment operator(++)
  • Assignment operator(=)
  • Equal operator(=)
  • Not equal operator(!=)
  • Decrement operator(--)

Random Access Iterator: A Random Access iterator is an iterator provides random access of an element at an arbitrary location. It has all the features of a bidirectional iterator plus it adds one more feature, i.e., pointer addition and pointer subtraction to provide random access to an element.

Providers Of Iterators

Iterator categories Provider
Input iterator istream
Output iterator ostream
Forward iterator
Bidirectional iterator List, set, multiset, map, multimap
Random access iterator Vector, deque, array

Iterators and their Characteristics

Iterator Access method Direction of movement I/O capability
Input Linear Forward only Read-only
Output Linear Forward only Write-only
Forward Linear Forward only Read/Write
Bidirectional Linear Forward & backward Read/Write
Random Random Forward & backward Read/Write

Disadvantages of iterator

  • If we want to move from one data structure to another at the same time, iterators won't work.
  • If we want to update the structure which is being iterated, an iterator won?t allow us to do because of the way it stores the position.
  • If we want to backtrack while processing through a list, the iterator will not work in this case.

Advantages of iterator

Following are the advantages of an iterator:

  • Ease in programming: It is convenient to use iterators rather than using a subscript operator[] to access the elements of a container. If we use subscript operator[] to access the elements, then we need to keep the track of the number of elements added at the runtime, but this would not happen in the case of an iterator.

Let's see a simple example:

Output:

      1 2 3 4 5                                                                                                          
      1 2 3 4 5                                                                                                          
      1 2 3 4 5 10                                                                                                       
      1 2 3 4 5 10 

In the above example, we observe that if we traverse the elements of a vector without using an iterator, then we need to keep track of the number of elements added in the container.

  • Code Reusability: A code can be reused if we use iterators. In the above example, if we replace vector with the list, and then the subscript operator[] would not work to access the elements as the list does not support the random access. However, we use iterators to access the elements, then we can also access the list elements.
  • Dynamic Processing: C++ iterators provide the facility to add or delete the data dynamically.

Let's see a simple example:

Output:

1 10 2 3 4 5

In the above example, we insert a new element at the second position by using insert() function and all other elements are shifted by one.

Difference b/w Random Access Iterator and Other Iterators

The most important difference between the Random access iterator and other iterators is that random access iterator requires '1' step to access an element while other iterators require 'n' steps.

C++ Iterators
Next TopicC++ Tutorial




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA