zoukankan      html  css  js  c++  java
  • 【函数】fill和fill_n填充之区别

    fill对区间填充

    原型:

    template < class ForwardIterator, class T >
    void fill ( ForwardIterator first, ForwardIterator last, const T& value )
    {
      while (first != last)  *first++ = value;
    }

    填充区间[first,last)

    示例:

    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int main () {
      vector<int> myvector (8);                       // myvector: 0 0 0 0 0 0 0 0
    
      fill (myvector.begin(),myvector.begin()+4,5);   // myvector: 5 5 5 5 0 0 0 0
      fill (myvector.begin()+3,myvector.end()-2,8);   // myvector: 5 5 5 8 8 8 0 0
    
      cout << "myvector contains:";
      for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
     
      return 0;
    }

    fill_n可指定填充的个数

    原型:

    template < class OutputIterator, class Size, class T >
      void fill_n ( OutputIterator first, Size n, const T& value )
    {
      for (; n>0; --n)  *first++ = value;
    }

    填充区间[first,first+n)

    n为要填充的元素个数

    示例:

    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int main () {
      vector<int> myvector (8,10);        // myvector: 10 10 10 10 10 10 10 10
    
      fill_n (myvector.begin(),4,20);     // myvector: 20 20 20 20 10 10 10 10
      fill_n (myvector.begin()+3,3,33);   // myvector: 20 20 20 33 33 33 10 10
    
      cout << "myvector contains:";
      for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
     
      return 0;
    }
  • 相关阅读:
    win10下 Ubuntu 18.04 LTS 的安装及 rlt8821ce网卡驱动的安装
    网络:tcp/ip
    数据结构分类
    面向对象的solid原则
    mysql数据库的操作
    锁机制
    设计模式在项目中的应用
    aop动态代理底层实现模拟
    变量在内存的分配_复习
    java内部类及类加载顺序
  • 原文地址:https://www.cnblogs.com/elesos/p/2757653.html
Copyright © 2011-2022 走看看