C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ Forward Iterator
Operations Performed on the Forward Iterator:
Where 'A' is a forward iterator type, and x and y are the objects of a forward iterator type, and t is an object pointed by the iterator type object. Let's see a simple example:
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
using namespace std;
template<class ForwardIterator> // function template
void display(ForwardIterator first, ForwardIterator last) // display function
{
while(first!=last)
{
cout<<*first<<" ";
first++;
}
}
int main()
{
vector<int> a; // declaration of vector.
for(int i=1;i<=10;i++)
{
a.push_back(i);
}
display(a.begin(),a.end()); // calling display() function.
return 0;
}
Output: 1 2 3 4 5 6 7 8 9 10 Features of the Forward Iterator:
Suppose 'A' and 'B' are the two iterators: A==B; // equality operator A!=B; // inequality operator
Suppose 'A' is an iterator and 't' is an integer variable: *A = t; t = *A;
Suppose 'A' is an iterator: A++; ++A; Limitations of the Forward Iterator:
Suppose 'A' is an iterator: A--; // invalid
Suppose 'A' and 'B' are the two iterators: A==B; // valid A>=B; // invalid
A+2; // invalid A+3; // invalid
|