I'm trying to code opposite action to this:
std::ostream outs; // properly initialized of course
std::set<int> my_set; // ditto
outs << my_set.size();
std::copy( my_set.begin(), my_set.end(), std::ostream_iterator<int>( outs ) );
it should be something like this:
std::istream ins;
std::set<int>::size_type size;
ins >> size;
std::copy( std::istream_iterator<int>( ins ), std::istream_iterator<int>( ins ) ???, std::inserter( my_set, my_set.end() ) );
But I'm stuck with the 'end' iterator -- input interators can't use std::advance and neither I can use two streams with the same source...
Is there any elegant way how to solve this? Of course I can use for loop, but maybe there's something nicer :)
You could derive from the istream_iterator<T>.
Though using Daemin generator method is another option, though I would generate directly into the set rather than use an intermediate vector.
Thanks for the ideas guys. Even when these things seem cool, I'm certainly not going to create new class / iterator for it ;-) I better understand why SGI decided to include "copy_n" algorithm now :)
Looking into this a bit I don't think reading directly into a set will work, as you need to call insert on it to actually add the elements (I could be mistaken, it is rather early in the morning here). Though looking at the STL documentation in VS2005 briefly I think something using the generate_n function should work, for instance:
Hopefully that's either solved your problem, or convinced you that the loop isn't that bad in the grand scheme of things.