以下内容来自www.cplusplus.com---------------------------------------------------

FILL:

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

Assigns val to all the elements in the range [first,last).

The behavior of this function template is equivalent to:

1
2
3
4
5
6
7
8
template <class ForwardIterator, class T>
void fill (ForwardIterator first, ForwardIterator last, const T& val)
{
while (first != last) {
*first = val;
++first;
}
}

MEMSET:


void * memset ( void * ptr, int value, size_t num );

Fill block of memorySets the first num bytes
of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).------------------------------------------------------------------------------------------------------



可以看出:fill是按元素填充而memset是按字节填充.

代码如下:
#include<iostream>
#include<cstring>
using namespace std;
int main(void)
{
int test[100];
fill(test,test+100,1);
cout<<"case 1:"<<endl;
for(int i=0;i<100;i++)
cout<<test[i]<<" ";
cout<<endl;
memset(test,1,sizeof(test));
cout<<"case 2:"<<endl;
for(int i=0;i<100;i++)
cout<<test[i]<<" ";
cout<<endl;
memset(test,1,100*sizeof(int));
cout<<"case 3:"<<endl;
for(int i=0;i<100;i++)
cout<<test[i]<<" ";
cout<<endl;
}

运行结果如下:




fill按元素填充,所以数组里有100个1;
memset按字节填充,int有四个字节,1*2^24+1*2^16+1*2^8+1*2^0=16843009,数组里有100个16843009

哦?所以说清零的话fill和memset一样了?

C++ fill 和memset的更多相关文章

  1. fill与memset的区别

    fill 的头文件是<iostream> 命名空间是std: 在memset(a,0(-1),sizeof(a))全部初值定为0或-1时两者是没有多大区别; 但是在初值为其他值得时候就不同 ...

  2. fill和memset的区别

    https://blog.csdn.net/xs18952904/article/details/75195412 memset只能初始化成为0或者-1,其他都要用fill来完成. #include& ...

  3. 【C++】fill函数,fill与memset函数的区别

    转载自:https://blog.csdn.net/liuchuo/article/details/52296646 memset函数 按照字节填充某字符在头文件<cstring>里面fi ...

  4. C++中std::fill/std::fill_n的使用

    There is a critical difference between std::fill and memset that is very important to understand. st ...

  5. fill 的用法

    博客 : http://blog.csdn.net/liuchuo/article/details/52296646 fill函数的作用是:将一个区间的元素都赋予val值.函数参数:fill(vec. ...

  6. 【ACM/ICPC2013】线段树题目集合(一)

    前言:前一段时间在网上找了一个线段树题目列表,我顺着做了一些,今天我把做过的整理一下.感觉自己对线段树了解的还不是很深,自己的算法能力还要加强.光练代码能力还是不够的,要多思考.向队友学习,向大牛学习 ...

  7. HDU 1045(Fire Net)题解

    以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定大小的棋盘中部分格子存在可以阻止互相攻击的墙,问棋盘中可以放置最多多少个可以横纵攻击炮塔. [题目分析] 这题本来在搜索专题 ...

  8. CDOJ 1270 Playfair

    模拟题,代码写得比较乱... #include<cstdio> #include<cstring> #include<cmath> #include<queu ...

  9. PAT A1004 Counting Leaves (30 分)——树,DFS,BFS

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

随机推荐

  1. Oracle分区表例子

    分区表 Oracle提供的分区方法有以下几种. 1.范围分区(range) 范围分区是应用范围比较广的表分区方式,它是以列的值的范围来作为分区的划分条 件,将记录存放到列值所在的 range分区中. ...

  2. 初识react native遇到的问题

    Andriod 使用react native时遇到的问题     打开现有项目报错: 从第一行Error可以知道是一个zip的压缩文件打不开,往下看应该是下载的Gradle文件有问题,提示也是让从新下 ...

  3. QML中使用相对路径

    QML里有三种路径: 默认使用URL路径. "qrc:///filepath".这用来索引资源文件. "file:///绝对路径".这用来索引本地文件系统中的文 ...

  4. window下phpstudy开启redis扩展

    注:一定要注意自己PHP的版本结构是64还是32位的!其次查看PHP Extension Build是NTS or TS! 1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本(特 ...

  5. iOS---开发实用传感器

    传感器 1.什么是传感器 传感器是一种感应\检测装置, 目前已经广泛应用于智能手机上 2.传感器的作用 用于感应\检测设备周边的信息 不同类型的传感器, 检测的信息也不一样 iPhone中的下面现象都 ...

  6. Linux修改ssh端口,减少暴力破解

    版本centos7   注意:操作时请勿断开当前的ssh连接,以免发生情况登陆不了.   1.修改的是 /etc/ssh/sshd_config 文件 打开文件之后会发现Port是注释掉的,默认为22 ...

  7. 简单探讨弹性布局flex

    css 弹性布局: 盒子模型: box-sizing属性1.content-box 正常的普通的盒子模型用padding和border会使盒子变大:(向外扩张)2.border-box 盒子模型,pa ...

  8. Proc datasets

    作用:控制数据集.Datasets 过程运行结果不输出,结果只有在日志里才能看到. 基本语法: proc datasets lib=work; quit; 用法: 1. 更改数据集 proc data ...

  9. Goldengate完成Mysql到Mysql的数据同步

    文档参考地址:http://blog.csdn.net/u010587433/article/details/49305019 需求: 使用Goldengate完成Mysql到Mysql的数据同步,源 ...

  10. cesium primitive方式 ————http://blog.sina.com.cn/s/blog_15e866bbe0102y0ji.html

    Cesium学习笔记-工具篇17-PrimitivePoint自定义渲染-点 (2018-08-28 16:12:06) 转载▼ 标签: cesium primitive 自定义渲染 shader c ...