The big advantage of iterator is that it offers an interface for any container type. Iterators are capable of making the algorithms independent of the container type. In other words, iterators are applied to every data type and every container.
Operations performed by iterators
An iterator is an advanced form of a pointer that is developed to iterate over the elements of the structures, etc. All iterators point towards a specific position of the container. It is just like an array index that identifies the slot number, but the index cannot perform operations on the content present inside the specific index. This is done by the pointers (iterators). Iterators have some basic operations like ordinary pointers. Let us elaborate on each operation.
Operator ( *)
It is represented by an asterisk symbol. This is used to return the value that is present in the current position.
This is a moving operator that works in such a way that it increments the current value and then goes to the next position in the specified container. Some iterators are also used for backward stepping using a reverse operator (- -).
Operator == and !=
It matches the position of the two iterators. An equivalent sign is for (is equal?), and the second one is a negation of the equivalent sign (is not equal to).
It is used to assign an iterator the position of a specific element it wants to refer to.
Difference between the iterators and the basic pointers
Pointers also have all the properties of operations we have described above. But still, there exists a difference between pointers and iterators. The difference is that the iterators may be smart pointers used to iterate and navigate more complex data structures. On the other hand, the pointers are data structure dependent and data type dependent too. Each container class has some basic functions that allow them to use iterators to point the elements. The basic two functions are given below.
These types of iterators specify the starting point among the elements of the containers. It is the point from which the function picks the values to start the iteration. It indicates the position of the first element.
This iterator represents the end of the elements in a container. This type of iterator is also known as a ‘past-the-end’ iterator.
This phenomenon also refers to the half-open range containing the first element but not the last one. This phenomenon has some advantages like it avoids handling empty ranges. For the empty ranges, begin is equal to the end().
Types of iterators and examples
There are 5 types of iterators:
Each algorithm has a different requirement for every iterator. For instance, the find() algorithm needs the ++ operator to be written so the iterator can navigate the container. It has access to read but doesn’t contain write access.
As the name input indicates, to give or to add something in the program. The information from any container is given to the C++ code. So this type is used by a program for reading the values from any container. This iterator only allows reading the content of the container but does not provide the authority to alter it. So these algorithms that use input iterators don’t modify the values of the container. These iterators are not strong like other iterators.
This will only increment the values. Now consider the example given below. Here we will use a container named “data”. Iter is an object that will navigate throughout the container.
This is for the end of stream iterator, which reads the values till the end of a file.
‘Output’ shows that the iterator is used for exporting the resultant values from the program to the container. This output iterator is like the input iterator up to some extent but differs in the feature of modifying the container. They are write-only iterators.
The forward iterators use the “++” operators to navigate over a container. In this case, the forward iterator goes only in the forward direction over the container, one element at a time. Unlike the previous two iterators, it goes through the sequence of elements in the same order every time we use this operator. This iterator is also known as a multiple-pass iterator. Now consider an example in which the square is calculated by moving forward using this iterator.
In this example, we have used a vector to find the square of elements. For this purpose, a vector class library is used with other initial libraries.
Forward iterator is initiated here. To take the square, a ‘for’ loop is used to take the square of the values. The parameters of the function take the value passed through the main function. Two variables are assigned the first and the last value of the array. This condition is fulfilled if the first value is not equivalent to the last one, then the first one is iterated.
The resultant value will display all the values and their squares, respectively.
Random means any arbitrary element. This involves some algorithms like a sort () and a binary search. As they both are capable of jumping directly to any random position in a container. We will use an example here to explain the working of a random access iterator. This code will output a random element of a vector we provide. This will take two parameters in the random function, just like the forward iterator. But unlike the forward iterator, it will take random first and random last elements. The randomly selected last element subtracts the randomly selected first element. Both these random values act as a begin () and an end() function.
The output will display the random values between 0 to 10, as we have described the range of numbers here.
A bidirectional iterator is the last type of iterators, and it contains all the features of a forward iterator and then supports the decrement operators in the form of prefix and postfix.
“C++ iterator” is an article that covers all the basic operations of iterators. It also demonstrates the type of iterators with suitable examples. Iterator plays an important role when you need to search or sort the data. Moreover, this tutorial also depicts read-write access of the iterator to any container.