Do fill_n fill and fill the same function, but with different parameter overloads?

I was looking for the <algorithm> fill and fill_n , and for me they seem to do about the same thing, but just are defined differently.

This is true, but if not, how are they different?

The wording of their descriptions seems to be about the same (as I read from MSDN at fill_n and fill ).

If they are the same, what is the advantage of having both of these features?

Does it just give the developer more options or faster than the other?

+4
source share
1 answer

This is not the same function, no. std::fill fills the range, given the start and end iterator. std::fill_n fills a certain number of elements, given the initial iterator and quantity. fill_n is useful for output iterators when there is no way to get the final iterator, for example, using 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); 
+21
source

Source: https://habr.com/ru/post/1391769/


All Articles