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;
    }
  • 相关阅读:
    jmap使用
    Spring boot后台搭建一使用MyBatis集成Mapper和PageHelper
    aspose.slides for java去除水印
    win10 解决Failed installing 'Tomcat8' service
    NoSQL数据库
    windows下Java调用可执行文件
    Neo4j CQL基本使用
    初识Neo4j
    (原)下载pubFig的python代码
    (原+译)win7远程连接ubuntu16.04
  • 原文地址:https://www.cnblogs.com/elesos/p/2757653.html
Copyright © 2011-2022 走看看