How to read arbitrary number of values using std::copy?

Posted by Miro Kropacek on Stack Overflow See other posts from Stack Overflow or by Miro Kropacek
Published on 2008-10-30T13:02:02Z Indexed on 2010/05/15 21:04 UTC
Read the original article Hit count: 156

Hi,

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 :)

© Stack Overflow or respective owner

Related posts about c++

Related posts about stl-algorithm