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 :)
Use:
Note the empty parameter:
Errr... copy_n() algorithm?
(Edited: I should have read the question closer...)
While somewhat suspect, you can get approximately the right behavior by having an entry in the file that will "fail" the first loop, then clear the fail bit on the stream and start reading more.
Data, without an explicit size, but like this
Fed to the code below seems to do what I meant, at least on VS2005 with STLPort.
Yes sdg but when I want to use another data structures in that file / stream? I should probably explicitly write here, I want to store another stuff after this set, this is the reason why I'm storing the size as well.
How about using an alternate iterator to do the traversal and then use a function object (or lambda) to fill in the container?
Of course this assumes you have a C++0x compliant compiler.
BTW, 'counting_iterator<>' is part of Boost.Iterator.
Or you could do this: