Last Updated : 25 Jul, 2024
After going through the template definition of various STL algorithms like std::find, std::equal, std::count, you must have found their template definition consisting of objects of type Input Iterator. So what are they and why are they used?
Input iterators are one of the five main types of iterators present in the C++ Standard Library, others being Output iterators, Forward iterator, Bidirectional iterator, and Random - access iterators.
Input iterators are considered to be the weakest as well as the simplest among all the iterators available, based upon their functionality and what can be achieved using them. They are the iterators that can be used in sequential input operations, where each value pointed by the iterator is read-only once and then the iterator is incremented.
One important thing to be kept in mind is that forward, bidirectional and random-access iterators are also valid input iterators, as shown in the iterator hierarchy above.
Salient Features
A == B // Checking for equality
A != B // Checking for inequality
3. Dereferencing: An input iterator can be dereferenced, using the operator * and -> as an rvalue to obtain the value stored at the position being pointed to by the iterator.
So, the following two expressions are valid if A is an input iterator:
*A // Dereferencing using *
A -> m // Accessing a member element m
4. Incrementable: An input iterator can be incremented, so that it refers to the next element in the sequence, using operator ++().
Note: The fact that we can use input iterators with increment operator doesn't mean that operator - -() can also be used with them. Remember, that input iterators are unidirectional and can only move in the forward direction.
So, the following two expressions are valid if A is an input iterator:
A++ // Using post increment operator
++A // Using pre increment operator
Swappable: The value pointed to by these iterators can be exchanged or swapped.
Practical implementation
After understanding its features and deficiencies, it is very important to learn about its practical implementation as well. As told earlier, input iterators are used only when we want to access elements and not when we have to assign elements to them. The following two STL algorithms can show this fact:
// Definition of std::find() template
InputIterator find (InputIterator first, InputIterator last,
const T& val)
{
while (first!=last)
{
if (*first==val) return first;
++first;
}
return last;
}
// Definition of std::copy() template
OutputIterator copy(InputIterator first, InputIterator last,
OutputIterator result)
{
while (first != last)
*result++ = *first++;
return result;
}
Limitations
After studying the salient features, one must also know its deficiencies as well which make it the weakest iterator among all, which are mentioned in the following points:
// C++ program to demonstrate input iterator
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1 = { 1, 2, 3, 4, 5 };
// Declaring an iterator
vector<int>::iterator i1;
for (i1 = v1.begin(); i1 != v1.end(); ++i1) {
// Accessing elements using iterator
cout << (*i1) << " ";
}
return 0;
}
Output:
1 2 3 4 5
The above is an example of accessing elements using input iterator, however, if we do something like:
*i1 = 7;
If A is an input iterator, thenA-- // Not allowed with input iterators
If A and B are input iterators, thenA == B // Allowed
A <= B // Not Allowed
If A and B are input iterators, thenA + 1 // Not allowed
B - 2 // Not allowed
So, the two above examples very well show when, where, why, and how input iterators are used practically.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4