Last Updated : 25 Jul, 2024
After going through the template definition of various STL algorithms like std::copy, std::move, std::transform, you must have found their template definition consisting of objects of type Output Iterator. So what are they and why are they used ?
Output iterators are one of the five main types of iterators present in C++ Standard Library, others being Input iterators, Forward iterator, Bidirectional iterator and Random - access iterators.
Output iterators are considered to be the exact opposite of input iterators, as they perform the opposite function of input iterators. They can be assigned values in a sequence, but cannot be used to access values, unlike input iterators which do the reverse of accessing values and cannot be assigned values. So, we can say that
input and output iterators are complementary to each other.
One important thing to be kept in mind is that forward, bidirectional and random-access iterators are also valid output iterators, as shown in the iterator hierarchy above.
Salient Features
A == B // Invalid - Checking for equality
A != B // Invalid - Checking for inequality
*A = 1 // Dereferencing using *
A -> m = 7 // Assigning a member element m
A++ // Using post increment operator
++A // Using pre increment operator
Practical implementation
After understanding its features and deficiencies, it is very important to learn about its practical implementation as well. As told earlier, output iterators are used only when we want to assign elements and not when we have to access elements. The following two STL algorithms can show this fact:
// Definition of std::move()Here, since the result is the iterator to the resultant container, to which elements are assigned, so for this, we cannot use input iterators and have made use of output iterators at their place, whereas for accessing elements, input iterators are used which only needs to be incremented and accessed.
template
OutputIterator move (InputIterator first, InputIterator last,
OutputIterator result)
{
while (first!=last)
{
*result = std::move(*first);
++result;
++first;
}
return result;
}
// Definition of std::find()So, since here, there was no need for assigning values to iterators and only we needed to access and compare the iterators, so there was no need of output iterator and we have, therefore, used only the input iterators only.
template
InputIterator find (InputIterator first, InputIterator last,
const T& val)
{
while (first!=last)
{
if (*first==val) return first;
++first;
}
return last;
}
So, the two above examples very well show when, where, why and how output iterators are used practically.
Limitations
After studying about the salient features, one must also know the deficiencies of output iterators as well, which are mentioned in the following points:
// C++ program to demonstrate output 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 elements using iterator
*i1 = 1;
}
// v1 becomes 1 1 1 1 1
return 0;
}
The above is an example of assigning elements using output iterator, however, if we do something like:
a = *i1 ; // where a is a variableSo, this is not allowed for output iterator, as they can only be the target in assignment. However, if you try this for above code, it will work, because vectors return iterators higher in hierarchy than output iterators. This big deficiency is the reason why many algorithms like std::find, which requires to access the elements in a range and check for equality cannot use output iterators for doing so, because we can't access values using it, so instead we make use of input iterators.
If A is an output iterator,thenA-- // Not allowed with output iterators
If A and B are output iterators, thenA == B // Not Allowed
A <= B // Not Allowed
If A and B are output iterators, thenA + 1 // Not allowed
B - 2 // 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