This function fills the range [first,last) with values generated by incrementing the value parameter. This is useful for filling an array with the integers 0...N-1.
template <class ForwardIterator, class T> void iota(ForwardIterator first, ForwardIterator last, T value) { while (first != last) *first++ = value++; }
Would it not be less demanding to require only pre-increament, and more efficient to use it?
template <class ForwardIterator, class T> void iota(ForwardIterator first, ForwardIterator last, T value) { while (first != last) { *first = value; ++ first; ++ value; } }
This avoids the creation of temporaries.
People/JeremySiek noted a typo in my original draft of iota, and also wrote: "Some compilers can get rid of the temporaries in some situations, but perhaps it is better not to rely on that."
template <class OutputIterator, class Size, class T> void iota_n (OutputIterator first, Size n, T value) { for (; n > 0; -- n) { *first = value; ++ first; ++ value; } }
Another possibility would be to supply function types for use with the existing generate algorithms.
template <class T> class Iota { public : Iota (T const& v) : value(v) { } T operator() () { return value ++; } private : T value; }; generate(v.begin(), v.end(), Iota<int>(0)); generate_n(back_inserter(v), 30, Iota<int>(0));
--John Potter
The same effect can already be achieved in Boost using [Counting Iterator]. So is it worth adding an algorithm as well?
--Alan Stokes
I think there is a better way to fill the container with subsequent numbers.
Following the [interval and integer iterator], you can define it as:
sktl::ivi( 0, 30 ).copy_back( v );
It allows also to give a sequence step as:
sktl::ivs<3>( 3, 18 ).copy_back( v );
The ivi function creates an interval of integer iterators with step=1. I think the integer_iterator class can be still genericized (and called, let's say, sequence_iterator) and the `int' type, on which it's based replaced with a parameter. This however would require resignation from the `tStep' parameter if the value type is not int.
So if you can do something as an algorithm or as an iterator adapter, prefer the algorithm, and provide the adaptor as a goody. --Marc Mutz