fill

  将value值填充整个区间,不能为OutputIterator,因为fill会用到first和last,outputIterator无法做相等的测试

template <class ForwardIterator, class T>
void fill( ForwardIterator first, ForwardIterator last,const T& value);

fill_n

  会将数值value填充[first,first+n),返回值为first+n,可以用outputIterator

template <class OutputIterator,class size,class T>
OutputIterator fill_n( OutputIterator first, OutputIterator last,size n,const T& value);

generate

template <class ForwardIterator, class Generator>
void generate ( ForwardIterator first, ForwardIterator last, Generator gen )
{
    while (first != last)
    {
        *first = gen();
        ++first;
    }
}

  该函数是使用gen函数产生的值填充范围内元素的值。参数gen是一个接受空参数,并返回一个对应的值的函数或者是函数对象之类的。

generate_n

template <class OutputIterator, class Size, class Generator>
OutputIterator generate_n ( OutputIterator first, Size n,const Generator gen )
{
    )
    {
        *first = gen();
        ++first;
        --n;
    }
}

  返回值是first+n,generate和generate_n中的gen会被调用n(或first+n)次,而非只在循环外调用一次,这一点很重要,因为Generate不一定会在每次调用时候都返回相同的结果,因此generate允许从文件读入,取局部状态的值并更改。

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;

class F
{
    private:
        static int i;
    public:
        int operator()()
        {
            return ++i;
        }
};
;

int main()
{
    vector<);
    generate(v.begin(),v.end(),rand);//调用c语言中的rand函数,填满v
    generate_n(v.begin(),,F());//调用函数对象
    for(auto i:v)
        cout<<i<<' ';
    cout<<endl;
    ;
}

填充整个区间(fill,fill_n,generate和generate_n)的更多相关文章

  1. STL: fill,fill_n,generate,generate_n

    fill Assigns the same new value to every element in a specified range. template<class ForwardIter ...

  2. C++ fill fill_n generate generate_n

    #include <iostream>#include <algorithm>#include <vector>#include <list>#incl ...

  3. 图像处理之泛洪填充算法(Flood Fill Algorithm)

    泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...

  4. HTML5 Canvas 填充与描边(Fill And Stroke)

    HTML5 Canvas 填充与描边(Fill And Stroke) 演示HTML5 Canvas Fill 与Stroke文字效果,基于Canvas如何实 现纹理填充与描边. 一:颜色填充与描边 ...

  5. 图像处理------泛洪填充算法(Flood Fill Algorithm) 油漆桶功能

    泛洪填充算法(Flood Fill Algorithm) 泛洪填充算法又称洪水填充算法是在很多图形绘制软件中常用的填充算法,最熟悉不过就是 windows paint的油漆桶功能.算法的原理很简单,就 ...

  6. STL_算法_填充新值(fill、fill_n、generate、generate_n)

    C++ Primer 学习中... 简单记录下我的学习过程 (代码为主) 全部容器适用 fill(b,e,v)             //[b,e)   填充成v fill_n(b,n,v)     ...

  7. c++ 容器填充指定长度(fill_n)

    #include <iostream> // cout #include <algorithm> // fill_n #include <vector> // ve ...

  8. 变易算法 - STL算法

    欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/mutating-algorithms.h ...

  9. STL--STL和她的小伙伴们:

    STL--概述: 标准模板库(StandardTemplateLibrary,STL),是C++程序设计语言标准模板库.STL是由Alexander Stepanov.Meng Lee和David R ...

随机推荐

  1. L1-056 猜数字

    一群人坐在一起,每人猜一个 100 以内的数,谁的数字最接近大家平均数的一半就赢.本题就要求你找出其中的赢家. 输入格式: 输入在第一行给出一个正整数N(≤10​4​​).随后 N 行,每行给出一个玩 ...

  2. 牛客练习赛 23 C 托米的位运算

    链接:https://www.nowcoder.com/acm/contest/156/C来源:牛客网 托米完成了1317的上一个任务,十分高兴,可是考验还没有结束 说话间1317给了托米 n 个自然 ...

  3. L259 合同

    We are satisfied with the terms of this contract for the most part, but we feel that your terms of p ...

  4. 爬虫系列1:python简易爬虫分析

    决定写一个小的爬虫系列,本文是第一篇,讲爬虫的基本原理和简易示例. 1.单个网页的简易爬虫 以下爬虫的主要功能是爬取百度贴吧中某一页面的所有图片.代码由主要有两个函数:其中getHtml()通过页面u ...

  5. "@P0"附近有语法错误解释及定位修复

    解释: "@P0" 指的是sql语句被预编译之后,其所对应的占位符的相对顺序位置,通俗的讲,就是它代表第几个占位符的位置,其中的0,并不是固定的,如果将定位符看做数组的话,这里的数 ...

  6. ES6 let和const 的相同点与区别

    相同点: 1. 一旦声明 值不能再改变,即不能重复声明. 2.不存在变量提升. 3.都存在暂时性死区. 不同点: 1.const声明的变量不得改变值,这意味着,const一旦声明变量,就必须立即初始化 ...

  7. [转]Peer-to-Peer Communication Across Network Address Translators

    Peer-to-Peer Communication Across Network Address Translators Bryan Ford Massachusetts Institute of ...

  8. Putty CentOS SSH 总是自动断开连接

    /********************************************************************** * Putty CentOS SSH 总是自动断开连接 ...

  9. kali的安装

    https://www.youtube.com/watch?v=sB3bchzlwio 注意视频中选择的是kali 2016.2版本, 在VMware中选择了Linux-Debian 8.*64(好像 ...

  10. Sublime 官方安装方法

    1. Crtl + ` : 进入控制台模式 2. 复制下面相应版本的代码,按Enter键运行 Sublime Text 3 import urllib.request,os,hashlib; h = ...