以下内容来自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. hihocoder 神奇字符串

    思路: 暴力,模拟. 实现: #include <iostream> #include <algorithm> #include <cstdio> #include ...

  2. CCF|分蛋糕|Java

    import java.util.Scanner; public class tyt { public static void main(String[] args) { Scanner in = n ...

  3. Android EditText 输入金额(小数点后两位)

    EditText edit = new EditText(context); InputType.TYPE_NUMBER_FLAG_DECIMAL //小数点型 InputType.TYPE_CLAS ...

  4. 在 Windows Server 上搭建 *** 服务端(转载加亲测)

    转载自:https://diveng.io/build-shadowsocks-server-on-windows-server.html 下面的教程建议大家使用第一种方法安装,说是比较简单.我则使用 ...

  5. workstation服务丢失 共享打不开 0x80070035

    这个问题困扰一个星期了,希望能帮到人.

  6. bash之数组

    Bash 提供索引和联想的一维数组变量.可用作任何变量 索引的数组 :内置的声明将显式声明数组.有没有最大限制 一个数组,也没有要求成员将索引或连续分配的大小.索引的数组 引用使用 (包括算术表达式) ...

  7. WebDAV协议

    WebDAV是一项基于 Http1.1 协议的通信协议.它扩展了HTTP 1.1,在Get.Post.Put.Delete 等HTTP标准方法外添加了新方法,使应用程序可对Web Server直接读写 ...

  8. 单文件组件.vue---父子组件通信

    每一个.vue 文件就是一个 组件,组件和组件相互组合,就成了一个应用,这就涉及到的组件和组件之间的通信,最常用的就是父子之间的通信.在vue 中, 在一个组件中通过 import 引入另一个组件,这 ...

  9. Mac OS X 中安装 brew

    不想被误导?直接看官方文档:   http://mxcl.github.com/homebrew/    先安装Git,打开一个shell cd /usr/local sudo mkdir homeb ...

  10. mac vim编辑器常用操作快捷方式

    0 行首$ (shift+6)行尾gg 文首G(shift+g) 文尾A(Shift+a)文尾,并编辑ctrl+f 向上翻页ctrl+b 向下翻页ctrl+u 向上翻半页ctrl+d 向下翻半页数字+ ...