- template <class ForwardIterator, class T>
- void fill(ForwardIterator first, ForwardIterator last, const T &value) {
- for(; first != last; ++first)
- *first = value;
- }
- template <class OutputIterator, class Size, class T>
- OutputIterator fill_n(OutputIterator first, Size n, const T &value) {
- for(; n > 0; --n, ++first)
- *first = value;
- return first;
- }
注:请注意输出区间的迭代器类型
当使用fill_n进行填充时,为防止 n 超过现有容器的大小,建议使用以下代码:
- int ia[] = {1, 2, 3};
- vector<int> iv(ia, ia + 3);
- fill_n(inserter(iv, iv.begin()), 5, 0);
转载于:https://blog.51cto.com/zxn990/1154478