Re: Extending std::iota function
Am 05.07.2011 21:51, schrieb red floyd:
On Jul 4, 6:33 pm, Ricky65<ricky...@hotmail.com> wrote:
In C++0x the iota function has been added which I find very useful for
testing. However, I find the ability to only increment by 1 to be very
limiting. Sometimes we don't want to fill a container or array with
consecutive integers, we want to control the increment. Consequently,
I feel it would be useful to provide an overload for iota with this
functionality. For example:
//version of iota where user can specify the increment
template<typename Iter, typename Initial_Value, typename Step>
void iota(Iter _First, Iter _Last, Initial_Value value, Step step)
{
for (; _First != _Last; ++_First, value += step)
*_First = value;
}
1. Identifiers with a leading underscore followed by an uppercase
are reserved to the implementation.
This is correct, but rather irrelevant here, because above
implementation can be considered as a possible library implementation.
2. I don't believe iterators are required to implement +=.
Correct as well, but this argument does not apply here, because
Initial_Value is no type that needs to satisfy some iterator requirements.
The function should probably read:
//version of iota where user can specify the increment
template<typename Iter, typename Initial_Value, typename Step>
void iota(Iter first, Iter fast, Initial_Value value, Step step)
{
for (; first != last; ++first, std::advance(value,step))
*first = value;
}
Rather not.
HTH & Greetings from Bremen,
- Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]