﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-zzg-文章分类-C++/C</title><link>http://www.cppblog.com/zzg/category/9882.html</link><description>Walking to the sky</description><language>zh-cn</language><lastBuildDate>Sun, 14 Jun 2009 17:27:10 GMT</lastBuildDate><pubDate>Sun, 14 Jun 2009 17:27:10 GMT</pubDate><ttl>60</ttl><item><title>c++ 中的fill  和memset</title><link>http://www.cppblog.com/zzg/articles/87414.html</link><dc:creator>zzg</dc:creator><author>zzg</author><pubDate>Thu, 11 Jun 2009 08:03:00 GMT</pubDate><guid>http://www.cppblog.com/zzg/articles/87414.html</guid><wfw:comment>http://www.cppblog.com/zzg/comments/87414.html</wfw:comment><comments>http://www.cppblog.com/zzg/articles/87414.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zzg/comments/commentRss/87414.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zzg/services/trackbacks/87414.html</trackback:ping><description><![CDATA[fill 的头文件是&lt;iostream&gt; 命名空间是std；<br />用法：<br />eg：<br />#include&lt;iostream&gt;<br />using namespace std;<br />int main()<br />{<br />         char s[100];<br />         fill(s,s+100,'a');<br />         for(int i=0;i&lt;100;i++)<br />          cout&lt;&lt;s[i];<br />       cout&lt;&lt;endl;<br />       system("pause");<br />}<br /><br />它的原理是把那一块单元赋成指定的值，与memset不同，<br />memset是按字节填充的例如：<br />这个例子可以很好的区别memset和fill：<br />#include&lt;iostream&gt;<br />using namespace std;<br />int main()<br />{<br />         int  d[100];<br />         fill(d,d+100,1);<br />         for(int i=0;i&lt;100;i++)<br />          cout&lt;&lt;d[i]&lt;&lt;" ";<br />       cout&lt;&lt;endl;<br />         memset(d,1,100*sizeof(int));<br />        for(int i=0;i&lt;100;i++)<br />        cout&lt;&lt;d[i]&lt;&lt;" ";<br />        cout&lt;&lt;endl;<br />        <br />       system("pause");<br />}<br />运行结果如下：<br />1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 <br />16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 <br />所以不难看出memset int 单元为1 时相当于<br />(1&lt;&lt;24)+(1&lt;&lt;16)+(1&lt;&lt;8)+1  =  16843009；<br /><br />&lt;&lt; 优先级低于+<br /><img src ="http://www.cppblog.com/zzg/aggbug/87414.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zzg/" target="_blank">zzg</a> 2009-06-11 16:03 <a href="http://www.cppblog.com/zzg/articles/87414.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>