Input Iterators in C++
What are input iterators?
Input iterators are used in sequence for carrying out input operations where each value is read-only. It is pointed by the iterator and further incremented.
All the iterators like forward, input, and bidirectional are also regarded as valid input iterators.
Input operators are not confined to only one type. There are multiple types. Let’s discuss this in detail.
Input iterators are the simplest and the weakest iterators. Such iterators are simple to use and valid for other functionalities that can be achieved by using them in our programs. They work on the phenomenon of iterating sequential inputs where each value is pointed by the iterator. Input iterators are read only once and later incremented.
To understand more clearly, consider these points that are easy to remember about input iterators:
- Input Iterators are used to read values from the container.
- Values in containers can be accessed by using dereferencing in the input container.
- It fails to change container values.
- It is uni-directional (one-way).
- It is incremental and cannot be decreased.
- Operators for an input iterators include increment operator (++), decrement operator (--), dereference operator (*), not equal operator (!=) and equal operator (==).
- An input Iterator is produced by the Istream.
- A bidirectional iterator, forward iterator, and all others are input iterators.
Let us look at the operations and properties of input iterators.
Properties | Expressions |
Copy-assignable, copy-constructible and destructible | X b(a); b=a |
Can be dereferenced | *x; |
Can be compared with equality and inequality operator | a==b; , a!=b |
Can be incremented | ++i; |
After considering the operations and properties, let us now closely examine the features of input iterators with the help of coding examples below.
Features of Input iterators
Let us look at a coding example to clearly visualize the working of input iterators. Let’s consider an example.
Example Code:
#include <iostream>
#include<vector>
#include<iterator>
using namespace std;
int main()
{
vector<int> vect{10,20,30,40,50};
vector<int>::iterator itr1,itr2;
itr1=vect.begin();
itr2=vect.begin()+1;
if(itr1==itr2)
{
std::cout << "Both the iterators are equal" << std::endl;
}
if(itr1!=itr2)
{
std::cout << "Both the iterators are not equal" << std::endl;
}
return 0;
}
1. Input iterators can only be used in those algorithms which allow single pass. A single-pass algorithm can go to all locations in a range when we may search any element in a container. It can iterate through all the locations at least once.
Input iterators are used to compare for equality with other iterators. We know that two iterators cannot point to a same position. But if they do, it clearly gives us a result that both the iterators are equal.
So, the following two expressions are valid if X and Y are input iterators:
For Example:
X == Y // Checking for equality X != Y // Checking for inequality
3. Input iterator can be used for dereferencing using * and -> operators for obtaining the value for storing the value that is being pointed by the iterator.
To visualize this, let us look at a coding example.
#include <iostream>
#include<vector>
#include<iterator>
using namespace std;
int main()
{
vector<int> vect{10,20,30,40};
vector<int>::iterator itr;
itr = vect.begin();
cout<<*itr;
return 0;
}
Output:
10
Explanation: Here, we can see that the vector is using begin() function. But it has not been assigned end() functional, since it is not an input iterator. The output 10 gives the idea that the *itr(dereferencing operator) points only the first element in the vector(dynamic array). So it prints only 10.
- An input iterator is swappable provided two inputs pointing at different locations.
Let us discover it with a coding example
#include<iostream>
#include<iterator>
using namespace std;
int main()
{
vector<int> vect{10,20,30,40};
vector<int>::iterator itr,itr1,temp;
itr = vect.begin();
itr1 = vect.begin()+1;
temp=itr;
itr=itr1;
itr1=temp;
cout<<*itr<<" ";
cout<<*itr1;
return 0;
}
Output:
20 10
Limitations of input iterators
- Input iterators are restricted to only increment. Decrementing is not allowed while using input iterator.
If X is used as an input iterator, then
X-- // Not allowed
2. Since it is unidirectional and is restricted to move only move forward, therefore, such iterators cannot be used in multi-pass algorithms, in which we need to process the container multiple times.
3. Input iterators can be used with equality operators (==) but cannot be used as relational operators like <=.
x == y // Allowed x <= y // Not Allowed
4. Input iterators resemble relational operators since they cannot be used with arithmetic operators like addition(+) and subtraction(-). This clearly implies that input iterators can move strictly forward and also they move one by one or sequentially.
If x and x are taken as input iterators, then
x+ 1 // Not allowed y - 3 // Not allowed