fill()和fill()_n函数还有memset()函数的比较

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))          
                          
                          
                          
                          
            

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值