Input Iterators
Lesson 14 of 23 in Coddy's C++ - Standard Template Library course.
Input Iterator is an iterators used to read the values from the container. The input iterators are the "weakest" of all of the iterators because they have limited functionality. But, this leads to input iterators being simple and easy to understand.
They are mostly used in sequential input operations, where each value is pointed by the iterator only once and then the iterator is incremented, most often using the ++ operator to increment the iterator by one.

As we can see, the forward iterator, bidirectional iterator and random access iterator are all valid input iterators.
So, we will learn about the features on an Input Iterator.
- Equality/Inequality operator
An input iterator can be compared by using an equality or inequality operator. The two iterators are equal only when both of the iterators are pointing to the same location.
A == B // Equality
A != B // InequalityLet's see an example where we will check whether two iterators are pointing to the same location.
vector<int> numbers = {1, 2, 3, 4, 5};
vector<int>::iterator it1, it2;
it1 = numbers.begin();
it2 = numbers.begin() + 1;
if(it1 == it2)
cout << "Both iterators are equal.";
else
cout << "Both iterators are not equal.";Output:
Both iterators are not equal.In the above example, we declare iterators called it1 and it2 using vector<int>::iterator. After that we assigned them values. The it1 iterator has the location of the first element by the begin() method. And the it2 iterator has the location of the second element because we incremented the value of the begin() method by one. That's why the output of the program is saying that the two iterators aren't the same.
- Dereferencing
An input iterator can be dereferenced, this is done using the*operator. If we output the dereferenced value of the iterator, we get the actual value of the element that the iterator is pointing to.
*A // Dereference with *vector<int> numbers = {1, 2, 3, 4, 5};
vector<int>::iterator it1, it2;
it1 = numbers.begin();
it2 = numbers.begin() + 1;
cout << *it1 << endl;
cout << *it2;Output:
1
2In the above example, we have the exact same case as the first one. The iterator it1 is pointing to the first element of the vector and the iterator it2 is pointing to the second element of the vector. When we output the dereferenced values of the iterators we get the actual values of the first and second element of the vector.
So, if we want to output the value of the variable or element that the iterator is pointing to we use the * operator
- Incrementable
An input iterator can be incremented, so it will refer to the next element in the sequence after the incrementation. For that we use the++operator
A++ // Post increment operator
++A // Pre increment operatorvector<int> numbers = {1, 2, 3, 4, 5};
vector<int>::iterator it1;
it = numbers.begin();
cout << "First value is: " << *it << " ";
it++;
cout << "Second value is: " << *it << " ";
++it;
cout << "Third value is: " << *it;Output:
1 2 3In the above example we declare an iterator it so it points to the first element of the vector. We output the first element of the vector and then we increment the iterator by one so it points to the second element and so on.
- Swappable
Two iterators pointing two different locations can be swapped. We can do that using a temporary variable like below.
vector<int> numbers = {1, 2, 3, 4, 5};
vector<int>::iterator it1, it2, temp;
it1 = numbers.begin();
it2 = numbers.begin() + 1;
cout << *it1 << " " << *it2 << endl;
temp = it1;
it1 = it2;
it2 = temp;
cout << *it1 << " " << *it2 << endl;Output:
1 2
2 1In the above example we declare the two iterators and we assign the location of the first element to the first iterator and the location of the second element to the second iterator. We output the two elements and we switch the values of the iterators, this is leading to the first iterator pointing to the second element and the second iterator pointing to the first element, as you can see in the output.
Challenge
MediumGiven 5 numbers from input use a vector and store the values. After that use an iterator with the begin() method and iterate through the vector incrementing the iterator each loop iteration
Input
10
20
30
40
50Output
10 20 30 40 50Try it yourself
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<int> numbers;
vector<int>::iterator it;
// Enter your code here
for(it = ; it != numbers.end(); )
{
}
return 0;
}