file i/o with stream_iterators - error

A

Aman

Hi,
could you please tell me why the following program gives a compilation
error ? what does this error mean ?
note also that when i derefernce the iterator , the contents are
displayed.

int main()
{
ifstream ifs("file") ;
istream_iterator<string> i_it(ifs) ;
copy(i_it.begin(), i_it.end(),ostream_iterator<string>(cout,"\n")) ;
}
___________________________
Complier error -
(SunOS 5.8 , g++ )
13 : no matching function for call to `istream_iterator<basic_stri
ng said:
,int>::begin ()'
13: no matching function for call to `istream_iterator<basic_stri
ng said:
,int>::end ()'
______________________
regards,
 
R

Rob Williscroft

Aman wrote in @mygate.mailgate.org:
Hi,
could you please tell me why the following program gives a compilation
error ? what does this error mean ?
note also that when i derefernce the iterator , the contents are
displayed.

int main()
{
ifstream ifs("file") ;
istream_iterator<string> i_it(ifs) ;
copy(i_it.begin(), i_it.end(),ostream_iterator<string>(cout,"\n")) ;

copy(
i_it,
istream_iterator<string>(),
ostream_iterator<string>(cout,"\n")
);


}
___________________________
Complier error -
(SunOS 5.8 , g++ )
13 : no matching function for call to `istream_iterator<basic_stri

Your trying to use an input iterator like its a container.

As I've done above use a default constructed istream_iterator<string>
as the end iterator.

13: no matching function for call to `istream_iterator<basic_stri

______________________


HTH

Rob.
 
D

David Rubin

Rob Williscroft wrote:

[snip]
copy(
i_it,
istream_iterator<string>(),
ostream_iterator<string>(cout,"\n")
);

Can you please explain why this works. I was under the impression that
the 'end' iterator had to be related to the 'begin' iterator in some
way. Thanks,

/david
 
A

Adam Fineman

David said:
Rob Williscroft wrote:

[snip]
copy(
i_it,
istream_iterator<string>(),
ostream_iterator<string>(cout,"\n")
);


Can you please explain why this works. I was under the impression that
the 'end' iterator had to be related to the 'begin' iterator in some
way. Thanks,

You snipped the declaration and definition of i_it, so here it is again:
------------------------------------------
ifstream ifs("file") ;
istream_iterator<string> i_it(ifs) ;
------------------------------------------

The 'end' iterator of any istream_iterator<T> is istream_iterator<T>().
I'll agree that the syntax is not intuitive, as it looks like a call
to the default constructor, but that's something we all have to live with.

As another example, here is a program that fills a container from the
standard input:

-------------------------------------
#include <vector>
#include <string>
#include <iterator>
#include <iostream>

using namespace std;

int
main()
{
vector<string> v;

copy(istream_iterator<string>(cin),
istream_iterator<string>(), // 'end' iterator
back_inserter(v));

cout << "Recieved " << v.size() << " strings as input.\n";

return 0;
}
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

David Rubin escribió:
Can you please explain why this works. I was under the impression that
the 'end' iterator had to be related to the 'begin' iterator in some
way. Thanks,

Because the operator = that compares two istream_iterator is written
that way, it gives a value of true when comparing one that has reached
his end to one default constructed. It's a simple solution that does not
need to define additional functions.

Regards.
 

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

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top