以下内容来自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. C#_JDBC连接数据库

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. chart.js图表 传值问题

    php:         $json['status'] = ture;                $json['list']=implode(',',$data);                ...

  3. 2017-11-29 HTML5样式、链接和表格

    HTML5样式.链接和表格HTML5列表<ol> 有序列表<ul> 无序列表<li> 列表项 <dl> 列表<dt> 列表项<dd&g ...

  4. Hadoop YARN学习监控JVM和实时监控Ganglia、Ambari(5)

    Hadoop YARN学习监控JVM和实时监控Ganglia.Ambari(5) 1.0 监控ResourceManager进程Java虚拟机中堆空间的特定部分. jstat工具,在JDK的bin目录 ...

  5. 在SQLServer 2005附加SQLServer 2008数据库异常处理

    远程服务器软件系统不算新,数据库是SQL Server 2005.本地开发基本是用新的软件系统.数据库采用SQL Server 2008. 这样在用远程服务器SQL 2005选择附加SQL 2008的 ...

  6. quazip非静态成员。。错误

    转载请注明出处:http://www.cnblogs.com/dachen408/p/7147155.html 问题:quazip非静态成员..错误 解决方案:quazip_global.h  第42 ...

  7. 程序员容易忽略的SQL Server错误集锦

    1.大小写 大写T-SQL 语言的所有关键字都使用大写,规范要求. 2.使用“;” 使用“;”作为 Transact-SQL 语句终止符.虽然分号不是必需的,但使用它是一种好的习惯,对于合并操作MER ...

  8. (转)淘淘商城系列——MyBatis分页插件(PageHelper)的使用以及商品列表展示

    http://blog.csdn.net/yerenyuan_pku/article/details/72774381 上文我们实现了展示后台页面的功能,而本文我们实现的主要功能是展示商品列表,大家要 ...

  9. SpringCloud 微服务框架

    学习资源:https://ke.qq.com/course/280057 知识体系分为以下几点: 1)使用Eureka搭建注册中心,包括 服务生产者.服务消费者(也称服务注册与发现): Zookeep ...

  10. 计算机内存数据存储基本原理----寄存器和RAM的电路基础

    计算机里存储数据主要有这几个部件:CPU里的寄存器和缓存.内存(内存条)和磁盘,这里我们主要简单讲下寄存器和内存条的基础实现电路. 在前面的文章<CPU怎么计算1+1----CPU计算的电路基础 ...