fill 的頭文件是<iostream> 命名空間是std;
用法:
eg:
#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");
}
它的原理是把那一塊單元賦成指定的值,與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");
}
運行結果如下:
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;
<< 優先級低于+