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;
    }
  • 相关阅读:
    你可能不知道的Linux/GNU bash sort多列排序功能
    设置字间距
    设置为灰度图
    点击短信中的url打开某个应用
    AchartEngine绘图引擎
    表格类似Excel
    自定义圆环progressbar
    高低版本方法兼容
    读取并创建excel文件(.xls)
    在android studio中导入github下载的工程
  • 原文地址:https://www.cnblogs.com/elesos/p/2757653.html
Copyright © 2011-2022 走看看