Boost logo

Boost :

From: John Torjo (john.lists_at_[hidden])
Date: 2004-05-17 03:43:25


Neal D. Becker wrote:

>Thanks for the advice. I think I have it now.
>
>Problem:
>
> A function F accepts a pair of stl-style iterators as input. A nullary
>functor G can generate an output. Arrange for F to call G n times without
>storing to an intermediate sequence.
>
>Example: G is a random number generator.
>
>I wonder if there is a more elegant approach? Here is what I came up with.
>
>
Yes, there is.
rtl (Range Templates Library) has a generate_iterator.

You have a generator, and a stopper. From these, you create an input iterator
sequence. The stopper tells you when you've reached end.

For instance:
// input iterator sequence - 20 fibonacci numbers
generated(fibonacci(), gen_n(20))

// input iterator sequence - fibonacci numbers up to 200
generated(fibonacci(), gen_upto(200))

You can use it like this:
rng::copy( generated(fibonacci(), gen_upto(200)), std::ostream_iterator<int>(std::cout," "));

The rtl (and the generate* examples) can be found at:
www.torjo.com/code/for_boost.zip

Best,
John

-- 
John Torjo
Freelancer
-- john_at_[hidden]
-- http://www.torjo.com/logview/ - viewing/filtering logs is just too easy!

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