zoukankan      html  css  js  c++  java
  • 算法之旅,直奔<algorithm>之十三 fill

    fill(vs2010)

    • 引言
    这是我学习总结<algorithm>的第十三篇,fill是一个很好的初始化工具。大学挺好,好好珍惜。。。
    • 作用
    fill  的作用是 给容器里一个指定的范围初始为指定的数据。
    In English, that is
    Fill range with value
    Assigns  val to all the elements in the range  [first,last).
    • 原型
    template <class ForwardIterator, class T>
      void fill (ForwardIterator first, ForwardIterator last, const T& val)
    {
      while (first != last) {
        *first = val;
        ++first;
      }
    }


    • 实验
    数据初始化为

    std::fill (myvector.begin(),myvector.begin()+4,5);

    std::fill (myvector.begin()+3,myvector.end()-2,8); 

    • 代码
    test.cpp
    #include <iostream>     // std::cout
    #include <algorithm>    // std::fill
    #include <vector>       // std::vector
    
    int main () {
    	std::vector<int> myvector (8);                       // myvector: 0 0 0 0 0 0 0 0
    
    	std::fill (myvector.begin(),myvector.begin()+4,5);   // myvector: 5 5 5 5 0 0 0 0
    	std::fill (myvector.begin()+3,myvector.end()-2,8);   // myvector: 5 5 5 8 8 8 0 0
    
    	std::cout << "myvector contains:";
    	for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    		std::cout << ' ' << *it;
    	std::cout << '
    ';
    	system("pause");
    	return 0;
    }


  • 相关阅读:
    小小杨的影视空间
    关于励志的事情
    关于2020年的总结
    关于心情不好的时候
    关于我的2020年
    单链表基本操作的实现
    原型模式
    android—安卓系统文件目录结构
    android——apk安装文件的组成结构
    android——项目的组成结构
  • 原文地址:https://www.cnblogs.com/fuhaots2009/p/3478615.html
Copyright © 2011-2022 走看看