input and forward iterator difference

M

Michal

Hallo group members
I wonder what is the difference between the two.

I can read that:
Input iterators are iterators especially designed for sequential input operations, where each value
pointed by the iterator is read only once and then the iterator is incremented.

I also can read:
A Forward Iterator is an iterator that corresponds to the usual intuitive notion of a linear sequence of values. It is possible to use
Forward Iterators (unlike Input Iterators and Output Iterators) in
multipass algorithms.

So my idea is that maybe dereferencing input iterator makes it
automatically move one step forward, while the same action on forward
iterator does not move it.

Unfortunatelly this is not true:
#include <fstream>
#include <iterator>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
ifstream f("/tmp/m.txt");
istream_iterator<int> a(f);
cout << *a << *a << *a;
cout << *a << *a << *a;
++a;
return 0 ;
}

Starting program: /tmp/p3
111111
Program exited normally.


So what is the difference then?

best regards,
Michal
 
V

Victor Bazarov

Michal said:
Hallo group members
I wonder what is the difference between the two.

I can read that:
Input iterators are iterators especially designed for sequential input operations, where each value
pointed by the iterator is read only once and then the iterator is incremented.

I also can read:
A Forward Iterator is an iterator that corresponds to the usual intuitive notion of a linear sequence of values. It is possible to use
Forward Iterators (unlike Input Iterators and Output Iterators) in
multipass algorithms.

So my idea is that maybe dereferencing input iterator makes it
automatically move one step forward, while the same action on forward
iterator does not move it.

Unfortunatelly this is not true:
#include <fstream>
#include <iterator>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
ifstream f("/tmp/m.txt");
istream_iterator<int> a(f);
cout << *a << *a << *a;
cout << *a << *a << *a;
++a;
return 0 ;
}

Starting program: /tmp/p3
111111
Program exited normally.


So what is the difference then?

I think the difference it that if you retain the forward iterator value,
then repeat the increments, you're guaranteed to get the same result,
with input/output iterators there is no such guarantee.

V
 
M

Marcel Müller

Michal said:
Hallo group members
I wonder what is the difference between the two.

A forward iteration can be repeated. Input iteration not.


Marcel
 
B

Bart van Ingen Schenau

Michal said:
Hallo group members
I wonder what is the difference between the two.

I can read that:
Input iterators are iterators especially designed for sequential
input operations, where each value pointed by the iterator is read
only once and then the iterator is incremented.

I also can read:
A Forward Iterator is an iterator that corresponds to the usual
intuitive notion of a linear sequence of values. It is possible to use
Forward Iterators (unlike Input Iterators and Output Iterators) in
multipass algorithms.

So my idea is that maybe dereferencing input iterator makes it
automatically move one step forward, while the same action on forward
iterator does not move it.

No. The difference comes to light when you have multiple copies of an
iterator over the same sequence.

For example:

#include <fstream>
#include <iterator>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
ifstream f("/tmp/m.txt");
istream_iterator<int> a(f);
istream_iterator<int> b(a);
cout << "iterate sequence using iterator a:\n";
cout << *a << '\n'; a++;
cout << *a << '\n'; a++;
cout << *a << '\n'; a++;

cout << "iterate same sequence again using iterator b:\n";
cout << *b << '\n'; b++;
cout << *b << '\n'; b++;
cout << *b << '\n'; b++;
return 0 ;
}

For forward iterators, the two sets of numbers that are printed must be
identical.
For input iterators, the two sets of numbers are most likely different
from each other. This means that incrementing input-iterator A also
affects the other input iterators that iterate over the same sequence.

So what is the difference then?

best regards,
Michal

Bart v Ingen Schenau
 
J

James Kanze

Hallo group members
I wonder what is the difference between the two.
I can read that:
Input iterators are iterators especially designed for
sequential input operations, where each value pointed by
the iterator is read only once and then the iterator is
incremented.
I also can read:
A Forward Iterator is an iterator that corresponds to the
usual intuitive notion of a linear sequence of values. It
is possible to use Forward Iterators (unlike Input
Iterators and Output Iterators) in multipass algorithms.
So my idea is that maybe dereferencing input iterator makes it
automatically move one step forward, while the same action on
forward iterator does not move it.

No. The difference is that incrementing a forward iterator has
no effect on any copies of it. Incrementing an input iterator
may. Thus, a == b implies ++ a == ++ b for a forward iterator,
but not for an input iterator.
Unfortunatelly this is not true:
#include <fstream>
#include <iterator>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
ifstream f("/tmp/m.txt");
istream_iterator<int> a(f);
cout << *a << *a << *a;
cout << *a << *a << *a;
++a;
return 0 ;
}

Try the following:

std::istringstream f( " 1 2 3 4 5 6 7 8 9" ) ;
std::istream_iterator< int > a( f ) ;
std::istream_iterator< int > b( a ) ;
std::cout << *a << std::endl ; ++ a ;
std::cout << *a << std::endl ; ++ a ;
std::cout << *a << std::endl ; ++ a ;
std::cout << *b << std::endl ; ++ b ;
std::cout << *b << std::endl ; ++ b ;

If istream_iterator were a forward iterator, the results would
be guaranteed to be 1 2 3 1 2. It's not, however, and they
won't be. (Try constructing b directly from f, as well, instead
of copy constructing it. The results will be rather surprising
as well.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top