Boost logo

Boost :

Subject: Re: [boost] [RangeEx] Using istream_range?
From: Neil Groves (neil_at_[hidden])
Date: 2008-10-21 04:24:18


Hi Robert,

You only have to supply one template parameter because the others will be
deduced. The first template parameter is the same as for istream_iterator,
it is the 'int' in your case.

On Tue, Oct 21, 2008 at 8:54 AM, Robert Jones <robertgbjones_at_[hidden]>wrote:

> Hi All
>
> I'd really appreciate an example of the use of istream_range from the
> Boost.Range library.
>
> I tried this
>
> #include <iostream>
> #include <iterator>
> #include "boost/range.hpp"
> #include "boost/range/istream_range.hpp"
> #include "boost/range/algorithm.hpp"
>
> void f( int i )
> {
> std :: cout << i << std :: endl;
> }
>
> int main( )
> {

This is incorrect...

>
> typedef std :: istream_iterator< int > in;
> boost :: for_each( boost :: istream_range< in >( std :: cin ), f );

It is even easier than that...

boost::for_each(boost::istream_range<int>(std::cin), f);

> return 0;
> }
>
> and other variations, but I don't think any of them are right! Do I really
> have to specify three template type parameters? The signature of the
> template is
>

<snip>
</snip>

And an example would be my super hurried test code that i haven't submitted
yet because it needs tidying:

#include <string>
#include <sstream>
#include <boost/range/istream_range.hpp>

namespace boost
{
    inline void test_istream_range()
    {
        std::string data("test");

        std::vector<char> reference;
        std::istringstream reference_s(data);

        std::copy(std::istream_iterator<char>(reference_s),
std::istream_iterator<char>(),
            std::back_inserter(reference));

        std::vector<char> test;
        std::istringstream test_s(data);
        copy(istream_range<char>(test_s), std::back_inserter(test));

        BOOST_CHECK( reference == test );
    }
} // namespace boost

Thanks, Rob.
> _______________________________

You are very welcome. I hope this helps. Let me know how you get on.

Best Wishes,

Neil Groves


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk