Boost logo

Boost :

From: Nathan Myers (ncm_at_[hidden])
Date: 1998-12-06 17:20:50

Beman Dawes writes:
> The background on the above is that when Greg looked at my min_rand
> class, he commented that it was very close to being useful as an STL
> compliant input iterator.
> I tried to work that out, but got hung up on termination. For
> example, if you wanted 1000 random numbers in a vector, you could
> write something like:
> min_rand rng;
> vector<long> vec( rng, ??? ); // oops! no way to specify end
> A third container-like class is needed, I think. end() would return
> 0,
> begin() would return an iterator with operator++() which returned
> ++rng for 1000 calls, then returned 0:
> min_rand rng;
> iter_counter itrc( rng, 1000 );
> vector<long> vec( itrc.begin(), itrc.end() );
> Anyhow, that is the problem Greg is thinking about.

That seems like extra apparatus. It's not a container.
Just define operator+ for it.

> min_rand rng;
> iter_counter itrc( rng );
> vector<long> vec( itrc, itrc + 1000 );

You compute *itrc lazily, so if you never say *(itrc + 1000),
you don't have to compute all the random numbers in between.

But even this seems like more stuff than is needed.
A better way might be to have a "singular value", as for
stream iterators. Then:

  vector<long> vec( iter_counter(min_rand(), 1000),
                    iter_counter() );

The singular value compares equal to any iter_counter that
is past the end of its range.

Nathan Myers
Free Web-based e-mail groups --

Boost list run by bdawes at, gregod at, cpdaniel at, john at