Re: [Dev-C++] how this program works?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Elizabeth B. <sog...@ya...> - 2002-08-31 19:51:03
|
vinay kumar <vch...@ya...> writes:
> Hello All,
>
> The following program copies the content of the file
> on to cout.
>
> ifstream ifile ("example_file");
> copy (istream_iterator <int, ptrdiff_t> (ifile),
> istream_iterator<int, ptrdiff_t> (),
> ostream_iterator<int> (cout) );
>
> could some one tell me how this program works?
>
> Regards
> Vinay.
You might be interested in Eckel's two books, Thinking in C++ volumes
I and II, especially II which address iterators although the whole
book is not yet complete:
Free versions are on the web at:
<http://www.bruceeckel.com/>
Analysis follows:
For information on the copy() function:
<http://wwwinfo.cern.ch/asd/lhc++/RW/stdlibcr/cop_4514.htm>
Apparently, copy just begins at one iterator and pumps each
iteration to the output iterator and both are increased.
We then need to look closer at the iostream iterators:
In <http://geneura.ugr.es/~gustavo/stl/istream_iterator.html> we see
the beginning of the definition of istream_iterator as:
template <class T, class Distance = ptrdiff_t>
class istream_iterator { ... }
And ostream_iterator:
template <class T>
class ostream_iterator { ... }
from:
<http://geneura.ugr.es/~gustavo/stl/ostream_iterator.html>
So, I'd say that an iterator is made from ifile, and each iteration is
pumped to ostream with each subsequent iterator checked against the
same type of istream that did not have a istream passed into it's
constructor.
As to how copy realizes that it's at the end of the file, though, I do
not know because it is just checking that it's not equal to the
standard constructor's istream iterator which defaults to standard
input when an istream is not passed into the constructor. It may be
that this is a special propery of istream_iterator.
Elizabeth
|