std::fill
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    |  | 该页由英文版wiki使用Google Translate机器翻译而来。 
 该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. | 
| 在头文件 <algorithm> 中定义
  | ||
| template< class ForwardIt, class T > void fill( ForwardIt first, ForwardIt last, const T& value ); | ||
分配给定的元素的范围内
value[first, last).原文:
Assigns the given 
value to the elements in the range [first, last).| 目录 | 
[编辑] 参数
| first, last | - |  元素的范围内进行修改 | 
| value | - |  被分配的值 | 
| 类型要求 | ||
| - ForwardIt必须满足ForwardIterator的要求。 | ||
[编辑] 返回值
(无)
[编辑] 复杂度
究竟
last - first分配.[编辑] 可能的实现
| template< class ForwardIt, class T > void fill(ForwardIt first, ForwardIt last, const T& value) { for (; first != last; ++first) { *first = value; } } | 
[编辑] 示例
 下面的代码使用
fill()设置为-1的所有整数的向量的元素 
 原文:
 The following code uses 
fill() to set all of the elements of a vector of integers to -1: 
 #include <algorithm> #include <vector> #include <iostream> int main() { int data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } std::vector<int> v1(data, data+10); std::fill(v1.begin(), v1.end(), -1); for (vector<int>::iterator it = v1.begin(); it != v1.end(); ++it) { std::cout << *it << " "; } std::cout << "\n"; }
输出:
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
[编辑] 另请参阅
| 将一个值赋给一定数目的元素 (函数模板) | |
| 将函数的结果保存于一个范围内 (函数模板) | |
| 将一个函数应用于某一范围的元素 (函数模板) | |


