fill 的头文件是<iostream> 命名空间是std;
示范代码:
- #include<iostream>
- using namespace std;
- int main()
- {
- char s[100];
- fill(s,s+100,'a');
- for(int i=0;i<100;i++)
- cout<<s[i];
- cout<<endl;
- system("pause");
- return 0;
- }
#include<iostream>
using namespace std;
int main()
{
char s[100];
fill(s,s+100,'a');
for(int i=0;i<100;i++)
cout<<s[i];
cout<<endl;
system("pause");
return 0;
}
它的原理是把那一块单元赋成指定的值,与memset不同,
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");
- return 0;
- }
#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");
return 0;
}
运行结果如下:
- 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
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;
为什么呢?
因为memset是以字节为单位就是对array指向的内存的4个字节进行赋值,每个都用ASCII为1的字符去填充,转为二进制后,1就是00000001,占一个字节。一个INT元素是4字节,合一起就是00000001000000010000000100000001,就等于16843009,就完成了对一个INT元素的赋值了。