c++ fill 和fill_n

template < class ForwardIterator, class T >

void fill ( ForwardIterator first, ForwardIterator last, const T& value );

Fill range with value

Sets value to all elements in the range [first,last).fill_n函数模板如下:
template < class OutputIterator, class Size, class T >
  void fill_n ( OutputIterator first, Size n, const T& value );

Fill sequence with value

Sets value to the first n elements in the sequence pointed by first.fill_n不检查是否有足够的空间来填充,切记使用是保证有足够的空间,不然会导致disaster。
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

int main()
{
    vector<int> vec(10);
    fill_n(vec.begin(),10,1);
    for(int i=0;i<vec.size();i++)
        cout<<vec[i]<<ends;
    fill(vec.begin(),vec.end(),2);
    for(int i=0;i<vec.size();i++)
        cout<<vec[i]<<ends;

    cout<<endl;
}

原文链接: https://www.cnblogs.com/youxin/archive/2012/06/16/2552254.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/52851

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 上午4:15
下一篇 2023年2月9日 上午4:15

相关推荐