memset是按字节填充
这个例子可以很好的区别memset和fill:
#include<iostream>
using namespace std;
int main()
{
int d[100];
fill(d,d+100,1);
for(int i=0;i<100;i++)
cout<<d[i]<<" ";
cout<<endl;
memset(d,1,100*sizeof(int));
for(int i=0;i<100;i++)
cout<<d[i]<<" ";
cout<<endl;
system("pause");
}
运行结果如下:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009
所以不难看出memset int 单元为1 时相当于
(1<<24)+(1<<16)+(1<<8)+1 = 16843009;
转载之:http://www.cppblog.com/zzg/articles/87414.html
fill函数就是单纯的赋值
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int haha[50]={0};
fill(haha,haha+20,6);
for(int i=0;i<50;i++)
printf("%d ",haha[i]);
return 0;
}
6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
--------------------------------
Process exited with return value 0
Press any key to continue . . .
fill_n函数() 第一个是开头 然后是给几个元素赋值之后就是赋值的内容
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int haha[50]={0};
fill_n(haha,6,6);
for(int i=0;i<50;i++)
printf("%d ",haha[i]);
return 0;
}
6 6 6 6 6 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
--------------------------------
Process exited with return value 0
Press any key to continue . . .
嗯。。。。。
int book[100];
总结就是fill (first,last,val)
fill(first,count,val)
memset(book,0,sizeof(book))