F
feminine.aura
I have to read a file containing integers into a vector. I could do
something like this:
ifstream data("file.dat");
istream_iterator<int> begin(data);
istream_iterator<int> end;
vector<int> c(begin,end);
Now this does what i want it to.
But I read in Scott meyer's effective C++ that if I try to get rid of
the second and the thir statement and do something like this
ifstream data("file.dat");
vector<int> c(istream_iterator<int>(data), istream_iterator<int>());
then this wouldn't work as expected because here the second line is
treated as a function declaration where c is the function which returns
a vector<int> and takes two parameters: a)istream_iterator<int> b) a
function pointer which doesnt take any arguments and return a
istream_iterator<int>
And that is the reason why this won't work.
But a friend of mine pointed me to this paper.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1798.html
where it says that such a declaration won't work, not because it is
considered as a function declaration but because an istream_iterator in
this case is considered as a forward iterator and that the
implementation of the constructor for vector which takes a forward
iterator is such that it is not possible to use an istream iterator
with them. I would request you to read the paper and correct me if I
misunderstood what the author said and let me know which one is right.
something like this:
ifstream data("file.dat");
istream_iterator<int> begin(data);
istream_iterator<int> end;
vector<int> c(begin,end);
Now this does what i want it to.
But I read in Scott meyer's effective C++ that if I try to get rid of
the second and the thir statement and do something like this
ifstream data("file.dat");
vector<int> c(istream_iterator<int>(data), istream_iterator<int>());
then this wouldn't work as expected because here the second line is
treated as a function declaration where c is the function which returns
a vector<int> and takes two parameters: a)istream_iterator<int> b) a
function pointer which doesnt take any arguments and return a
istream_iterator<int>
And that is the reason why this won't work.
But a friend of mine pointed me to this paper.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1798.html
where it says that such a declaration won't work, not because it is
considered as a function declaration but because an istream_iterator in
this case is considered as a forward iterator and that the
implementation of the constructor for vector which takes a forward
iterator is such that it is not possible to use an istream iterator
with them. I would request you to read the paper and correct me if I
misunderstood what the author said and let me know which one is right.