With C++11, the STL has now a std::iota
function (see a reference). In contrast to std::fill_n
, std::generate_n
, there is no std::iota_n
, however. What would be a good implementation for that? A direct loop (alternative 1) or delegation to std::generate_n
with a simple lambda expression (alternative 2)?
Alternative 1)
template<class OutputIterator, class Size, class T>
OutputIterator iota_n(OutputIterator first, Size n, T value)
{
while (n--)
*first++ = value++;
return first;
}
Alternative 2)
template<class OutputIterator, class Size, class T>
OutputIterator iota_n(OutputIterator first, Size n, T value)
{
return std::generate_n(first, n, [&](){ return value++; });
}
Would both alternatives generate equivalent code with optimizing compilers?
UPDATE: incorporated the excellent point of @Marc Mutz to also return the iterator at its destination point. This is also how std::generate_n
got updated in C++11 compared to C++98.
As a random example, I compiled the following code with
g++ -S -O2 -masm=intel
(GCC 4.7.1, x86_32):Here
iota_n
is the first version andiota_m
the second. The assembly is in all three cases this:With
-O3
, the three versions are also very similar, but a lot longer (using conditional moves andpunpcklqdq
and such like).