Home >>C++ Tutorial >C++ Iterators

C++ Iterators

Iterators in C++

An object (like a pointer) that generally point towards an element that is inside the container is known as Iterators in C++. The iterators can generally be used to move through the contents of the container. We can say that the iterators are like a pointer that is pointing towards some location and the content can be accessed at that particular location just by using them.

A critical role is played by the Iterators in connecting the algorithm with containers inclusding00 the manipulation of data that has been stored inside the containers. Pointer is the most obvious form of an iterator. A pointer can generally iterate through the elements in an array by using the increment operator (++) as pointer is known to point to the elements in an array. Please note that all the iterators don’t have similar functionality as pointers.

Types of iterators

Judging upon the functionality of the iterators, they are generally classified into five major categories that are explained in brief here for your understanding:

  • Input Iterators
  • Output Iterators
  • Forward Iterator
  • Bidirectional Iterators
  • Random-Access Iterators

Input Iterators

These types of iterators are known as the weakest of all the iterators as they have a very limited functionality. This type of iterator are known to be used only once in a single-pass algorithm, in simple words these are the algorithms that generally process the container in a sequential order so that none of the element is allowed to be accessed more than once.

Output Iterators

This type of iterators are very similar to the input iterators as they are also known to have a very limited functionality and just like input iterators their only use is in a single-pass algorithm, instead of accessing elements they are used for being assigned elements.

Forward Iterator

These types of iterators are generally higher in the table of hierarchy as compared to the input and output iterators. They generally consist of all the features that are present in these two iterators. You can get an idea about their functionality through their name as they can generally move in forward direction with a limitation of taking one step at a particular time.

Bidirectional Iterators

These types of iterators are known to consist every features of forward iterators. But they are very useful as compared to the forward iterator as this iterator is known to overcome the drawback of forward iterators. This is because they have the ability to move in both the directions and they are named bidirectional.

Random-Access Iterators

These types of iterators are known to be the most powerful iterators among all. They generally don’t have any limitations like moving sequentially. You can get an idea through their name that they are allowed to access any element randomly that is inside the container. These iterators are the only one that has the same fun functionality as the pointers.

Benefits/Advantages of Iterators

There are generally very few that can prove that iterators are extremely useful to the programmers and encourage them to use it very profoundly. Here are some of the benefits of using the iterators depicted below:

  1. Convenience in programming : It is always recommended that it is generally better to use the iterators in order to iterate through the contents of containers. Here is the demerit that will pop-up if the programmers will not use an iterator and access elements using [ ] operator doing this will give birth to the need of varying sizes of the container, on the other hand, programmers can simply make the use of member function end () with iterators to iterate through the contents without having to worry about anything.
  2. Code reusability : The iterators are known to support the reusability of as code and this provides the feasibility of not writing the same code again and again to the programmers as it enhances the efficiency in them. This code reusability has many uses including that this code can generally be used to access elements of any container.
  3. Dynamic processing of container : Iterators generally provides the ability to the programmers that they can generally add or remove the elements elements from the container as this delivers the ease of access to the programmers and enhance the working capability of an individual.

Popular Tutorials