简体   繁体   English

fill_n和fill是否填充相同的函数,但参数重载不同?

[英]Are fill_n and fill the same function but with different parameter overloads?

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. 我正在研究<algorithm>fillfill_n函数,对我来说,他们似乎做了同样的事情,但只是定义不同。

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 ). 他们描述的措辞似乎大致相同(我在MSDN上读取fill_nfill )。

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填充一个范围。 std::fill_n fills a certain number of elements, given a start iterator and a quantity. 给定启动迭代器和数量, std::fill_n填充一定数量的元素。 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 : fill_n对于输出迭代器非常有用,当你无法获得结束迭代器时,例如使用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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM