std::fill_n
来自cppreference.com
|
|
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
| 定义于头文件 <algorithm>
|
||
| template< class OutputIt, class Size, class T > void fill_n( OutputIt first, Size count, const T& value ); |
(C++11 前) (C++11 起) |
|
分配给给定的值
valuecount如果first开始的范围内,在第一count>0元素。什么都不做,否则.....原文:
Assigns the given value
value to the first count elements in the range beginning at first if count>0. Does nothing otherwise.目录 |
[编辑] 参数
| first | - | 修改的范围内的元素开始
原文: the beginning of the range of elements to modify |
| count | - | 要修改的元素的数量
|
| value | - | 被分配的值
|
| 类型要求 | ||
-OutputIt 必须满足 OutputIterator 的要求。
| ||
[编辑] 返回值
(无)(C++11 前)
如果
count>0,first否则分配的最后一个元素的迭代器一个过去。 (C++11 起)原文:
Iterator one past the last element assigned if
count>0, first otherwise. (C++11 起)[编辑] 复杂度
究竟
count任务,count>0.原文:
Exactly
count assignments, for count>0.[编辑] 可能的实现
template<class OutputIt, class Size, class T> OutputIt fill_n(OutputIt first, Size count, const T& value) { for (Size i = 0; i < count; i++) { *first++ = value; } return first; } |
[编辑] 示例
下面的代码使用
fill_n()分配-1,上半年的向量整数
原文:
The following code uses
fill_n() to assign -1 to the first half of a vector of integers:
#include <algorithm> #include <vector> #include <iostream> int main() { std::vector<int> v1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; std::fill_n(v1.begin(), 5, -1); for (vector<int>::iterator it = v1.begin(); it != v1.end(); ++it) { std::cout << *it << " "; } std::cout << "\n"; }
输出:
-1 -1 -1 -1 -1 5 6 7 8 9
[编辑] 另请参阅
| 将一个值赋给一个范围内的元素 (函数模板) |