I was looking into <algorithm>
's fill
and fill_n
function, and to me they seem to do about the same thing but are just defined differently.
Is this true, and if not, how are they different?
The wording for their descriptions seem to be about the same (that I read from MSDN at fill_n
and fill
).
If they are the same, what is the advantage of having both of these functions available?
It is to just give a developer more options, or is one faster than the other?
They're not the same function, no. std::fill
fills a range, given a start and end iterator. std::fill_n
fills a certain number of elements, given a start iterator and a quantity. fill_n
is useful for output iterators, when there's no way for you to get an end iterator, such as with std::ostream_iterator
:
std::fill( std::ostream_iterator<int>(std::cout), ???, x);
^^^
what do I put here?
std::fill_n( std::ostream_iterator<int>(std::cout), 25, x);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With