<ni*********@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
>thanks!
NB: please don't top-post, see
http://www.parashift.com/c++-faq-lit...t.html#faq-5.4
>an extra question:
how to read the first n elements of the file using the second solution?
"
typedef std::istream_iterator<ItemTypeInItem;
std::vector<ItemTypeconst items( (InItem(ifs)), InItem() );
"
Sadly, I can't think of an obvious way to read a fixed number
of elements using the above approach - not using the standard
library alone.
Regrettably, I think in this case I would revert to using a loop:
std::vector<ItemTypeitems(n);
for( unsigned i = 0 ; i<n ; ++i )
ifs >items[i];
Various C++ library implementations provide extensions that would also
help with this, for example:
http://www.sgi.com/tech/stl/copy_n.html
std::vector<ItemTypeitems;
copy_n( istream_iterator<ItemType>(ifs), n, back_inserter(items) );
It would also be possible to write an iterator wrapper to allow
writing:
typedef count_iterator<std::istream_iterator<ItemType InItem;
std::vector<ItemTypeconst items( (InItem(ifs)), InItem(n) );
I don't know if the planned/official C++ library extensions (TR1, TR2,
or C++0x) include a facility that would support any of the above...
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <
http://www.brainbench.com