fill()和fill()

xiaoxiao2021-02-28  126

fill函数的原理是把那一块单元赋成指定的值,与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;

转载之: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 . . .                             

fill函数的作用是:将一个区间的元素都赋予val值。函数参数:fill(first,last,val);//first为容器的首迭代器,last为容器的末迭代器,val为将要替换的值。 fill_n函数 的作用是:给你一个起始点,然后再给你一个数值count和val。把从起始点开始依次赋予count个元素val的值。 注意: 不能在没有元素的空容器上调用fill_n函数  一点点转载来之:     http://blog.sina.com.cn/s/blog_933dc4350100y9xn.html

嗯。。。。。

int book[100];

总结就是fill (first,last,val)   

                fill(first,count,val)

               memset(book,0,sizeof(book))                                                                                                                                   

转载请注明原文地址: https://www.6miu.com/read-44542.html

最新回复(0)