Last Updated : 25 Apr, 2019
After going through the template definition of various STL algorithms like
std::search,
std::search_n,
std::lower_bound, you must have found their template definition consisting of objects of type
Forward Iterator. So what are they and why are they used ?
Forward iteratorsare one of the five main types of iterators present in C++ Standard Library, others being
Input iterators,
Output iterator,
Bidirectional iteratorand
Random - access iterators. Forward iterators are considered to be the
combination of input as well as output iterators. It provides support to the functionality of both of them. It permits values to be both accessed and modified.
One important thing to be kept in mind is that
bidirectionaland
random-accessiterators are also valid forward iterators, as shown in the iterator hierarchy above.
Salient Features
A == B // Checking for equality A != B // Checking for inequality
// C++ program to demonstrate forward 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) {
// Assigning values to locations pointed by iterator
*i1 = 1;
}
for (i1 = v1.begin(); i1 != v1.end(); ++i1) {
// Accessing values at locations pointed by iterator
cout << (*i1) << " ";
}
return 0;
}
Output:
1 1 1 1 1So, as we can see here we can both access as well as assign value to the iterator, therefore the iterator is at least a forward iterator( it can be higher in hierarchy also).
A++ // Using post increment operator ++A // Using pre increment operator
Practical implementation
After understanding its features, it is very important to learn about its practical implementation as well. As told earlier, forward iterators can be used both when we want to access elements and also when we have to assign elements to them, as it is the combination of both input and output iterator. The following two STL algorithms can show this fact:
// Definition of std::replace()
template void replace(ForwardIterator first, ForwardIterator last,
const T& old_value, const T& new_value)
{
while (first != last) {
if (*first == old_value) // L1
*first = new_value; // L2
++first;
}
}
Here, we can see that we have made use of forward iterators, as we need to make use of the feature of both input as well as output iterators. In L1, we are required to dereference the iterator first as a rvalue (input iterator) and in L2, we are required to dereference first as a lvalue (output iterator), so to accomplish both the tasks, we have made use of forward iterators.
// Definition of std::reverse_copy()
template OutputIterator reverse_copy(BidirectionalIterator first,
BidirectionalIterator last,
OutputIterator result)
{
while (first != last)
*result++ = *--last;
return result;
}
Here, we can see that we have declared last as a bidirectional iterator, and not a forward iterator since we cannot decrement a forward iterator as done in case of last, so we cannot use it in this scenario.As we know that forward iterators are the combination of both input and output iterators, so if any algorithm involves the use of any of these two iterators, then we can use forward iterators in their place as well, without affecting the program. So, the two above examples very well show when, where, why and how forward iterators are used practically.
Limitations
After studying the salient features, one must also know its deficiencies as well although there are not as many as there are in input or output iterators as it is higher in the hierarchy.
If A is a forward iterator, then A-- // Not allowed with forward iterators
If A and B are forward iterators, then A == B // Allowed A <= B // Not Allowed
If A and B are forward iterators, then A + 1 // Not allowed B - 2 // Not allowed
If A is a forward iterator, then A[3] // Not allowed
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