﻿<?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++博客-无法递归的五点半-随笔分类-使用标准库和Boost库</title><link>http://www.cppblog.com/lambdacpp/category/1786.html</link><description>for_each(day.begin(),day.end(),bind2nd(Add(),me))</description><language>zh-cn</language><lastBuildDate>Mon, 19 May 2008 19:14:21 GMT</lastBuildDate><pubDate>Mon, 19 May 2008 19:14:21 GMT</pubDate><ttl>60</ttl><item><title>boost::mem_fn 从成员函数指针到函数对象</title><link>http://www.cppblog.com/lambdacpp/archive/2007/07/24/28688.html</link><dc:creator>五点半</dc:creator><author>五点半</author><pubDate>Tue, 24 Jul 2007 04:34:00 GMT</pubDate><guid>http://www.cppblog.com/lambdacpp/archive/2007/07/24/28688.html</guid><wfw:comment>http://www.cppblog.com/lambdacpp/comments/28688.html</wfw:comment><comments>http://www.cppblog.com/lambdacpp/archive/2007/07/24/28688.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lambdacpp/comments/commentRss/28688.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lambdacpp/services/trackbacks/28688.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp; C++通过范型编程倒是映入了大量函数编程的思想和手法,但终究强类型的语言,有时代码就不是那么优雅了.<br>&nbsp;&nbsp;&nbsp; 一般情况下函数指针和函数对象(functor)是可以互换的,如std::for_each的第三个参数,但标准库中有4个函数配接器(not1,not2,bind1st,bind2nd)是不能接受函数指针,必须使用std::ptr_fun将函数指针封装成函数对象.还有一种情况是函数指针指向的函数的参数类型与函数模板需要的不匹配,如果要通过for_each调用每个Iterator的解引用(dereference)的对象的某个成员函数.直接使用诸如ClassA::*member_func的成员函数指针显然是想当然的做法,调用的语法都不一致,结果是一大堆编译错误信息.没有关系可以使用std::mem_fun和std::mem_fun_ref.如for_each(classAVector.begin(),classAVector.end(),mem_fun_ref(ClassA::*member_func)).标准库中的这一套函数适配器(functor adapters)使用起来要充分考虑使用的场合.不同的情况使用不同配接器.特别是您要区分for_each的容器是Container&lt;T&gt;还是Container&lt;T*&gt;,前者使用std::mem_fun_ref,后者使用std::mem_fun.这一段的详细讨论可一参照Effective STL的第40条和第41条.(个人感觉这几个配接器的名字真有的...fun).<br>&nbsp;&nbsp;&nbsp; 如果您实在是有点受不了或者你不能确定容器中究竟放的是什么而且你愿意使用一下准标准库,就可以考虑boost库的mem_fn.boost::mem_fn也不是很特别的boost成员.使用boost::mem_fn好处：不用关心容器中是T还是T*甚至是boost::share_ptr&lt;T&gt;,另外买一赠一fn比fun更好听一点.下面抄录boost的帮助代码就很说明问题了.<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">struct&nbsp;X<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;f();<br>};<br><br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;g(std::vector</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">X</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;v)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;std::for_each(v.begin(),&nbsp;v.end(),&nbsp;boost::mem_fn(</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">X::f));<br>};<br><br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;h(std::vector</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">X&nbsp;</span><span style="color: #000000;">*&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;v)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;std::for_each(v.begin(),&nbsp;v.end(),&nbsp;boost::mem_fn(</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">X::f));<br>};<br><br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;k(std::vector</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">boost::shared_ptr</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">X</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;v)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;std::for_each(v.begin(),&nbsp;v.end(),&nbsp;boost::mem_fn(</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">X::f));<br>};<br></span></div>
<br>   <img src ="http://www.cppblog.com/lambdacpp/aggbug/28688.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lambdacpp/" target="_blank">五点半</a> 2007-07-24 12:34 <a href="http://www.cppblog.com/lambdacpp/archive/2007/07/24/28688.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>将map中的数据导入到vector中</title><link>http://www.cppblog.com/lambdacpp/archive/2006/10/23/14050.html</link><dc:creator>五点半</dc:creator><author>五点半</author><pubDate>Mon, 23 Oct 2006 13:57:00 GMT</pubDate><guid>http://www.cppblog.com/lambdacpp/archive/2006/10/23/14050.html</guid><wfw:comment>http://www.cppblog.com/lambdacpp/comments/14050.html</wfw:comment><comments>http://www.cppblog.com/lambdacpp/archive/2006/10/23/14050.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/lambdacpp/comments/commentRss/14050.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lambdacpp/services/trackbacks/14050.html</trackback:ping><description><![CDATA[要将一个map&lt;T1,T2&gt;中的数据导入到一个vector&lt;T2&gt;中，可以考虑使用标准库中的transform,但map&lt;T1,T2&gt;::iterator与vector&lt;T2&gt;::iterator是不匹配的，受《Effective STL》第20条启发写个map2vector的functor，可以解决这个问题（不过这个还不算真正的meta-programming）：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">map</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">vector</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iterator</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);"> std;<br /><br /></span><span style="color: rgb(0, 0, 255);">struct</span><span style="color: rgb(0, 0, 0);"> map2vector<br />{<br />    template</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">typename T</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">const</span><span style="color: rgb(0, 0, 0);"> typename T::second_type</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">operator</span><span style="color: rgb(0, 0, 0);">()(</span><span style="color: rgb(0, 0, 255);">const</span><span style="color: rgb(0, 0, 0);"> T</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);"> p)<br />        {<br />            </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> p.second;<br />        }<br />};<br /><br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main()<br />{<br />    map</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"> m;<br />    vector</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"> v;<br /><br />    m[</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">key1</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">sdf111</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br />    m[</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">k2</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">sdf11</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br />    m[</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">k3</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">sdf2</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br /><br />    transform(m.begin(),m.end(),back_inserter(v),map2vector());<br />    copy(v.begin(),v.end(),ostream_iterator</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">(cout,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">));<br /></span><span style="color: rgb(0, 0, 255);">    return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />}<br /></span></div><br /><br /><img src ="http://www.cppblog.com/lambdacpp/aggbug/14050.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lambdacpp/" target="_blank">五点半</a> 2006-10-23 21:57 <a href="http://www.cppblog.com/lambdacpp/archive/2006/10/23/14050.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Boost库的format</title><link>http://www.cppblog.com/lambdacpp/archive/2006/05/25/7610.html</link><dc:creator>五点半</dc:creator><author>五点半</author><pubDate>Wed, 24 May 2006 17:46:00 GMT</pubDate><guid>http://www.cppblog.com/lambdacpp/archive/2006/05/25/7610.html</guid><wfw:comment>http://www.cppblog.com/lambdacpp/comments/7610.html</wfw:comment><comments>http://www.cppblog.com/lambdacpp/archive/2006/05/25/7610.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lambdacpp/comments/commentRss/7610.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lambdacpp/services/trackbacks/7610.html</trackback:ping><description><![CDATA[特别Boost库中的format用于代替printf函数。其使用方法非常简单。<br /><ol><li>定义一个format类，并指定字符串的各式(相当于printf的第一个参数)</li><ul><li>boost::format fs("Test String:%s,%d");</li></ul><li>通过%运算符带入参数(%s表示字符串,%d表示整数,与printf非常类似)</li><ul><li>fs%"string"%121;</li></ul><li>通过&lt;&lt;输出(format重载了&lt;&lt;运算符)或通过str()函数获取结果</li><ul><li>std::cout&lt;&lt;fs; //输出Test String:string,121</li><li>std::string result = fs.str();</li></ul></ol>一些函数:<br /><ul><li>str() 获取结果字符串</li><li>clear() 清空已经带入的参数</li><li>parse(str::string s)清空已经带入的参数,并将s加在各式字符串之后</li></ul>有一点要注意:在获取结果字符串或&lt;&lt;输出时参数一定要完整带入,不然将抛出异常。<br /><br />此处仅仅抛砖引玉，如要使用请参考:http://www.boost.org/libs/format/doc/format.html<br /><br /><img src ="http://www.cppblog.com/lambdacpp/aggbug/7610.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lambdacpp/" target="_blank">五点半</a> 2006-05-25 01:46 <a href="http://www.cppblog.com/lambdacpp/archive/2006/05/25/7610.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++中的typename关键字(想哪儿说哪儿)</title><link>http://www.cppblog.com/lambdacpp/archive/2006/05/25/7607.html</link><dc:creator>五点半</dc:creator><author>五点半</author><pubDate>Wed, 24 May 2006 17:16:00 GMT</pubDate><guid>http://www.cppblog.com/lambdacpp/archive/2006/05/25/7607.html</guid><wfw:comment>http://www.cppblog.com/lambdacpp/comments/7607.html</wfw:comment><comments>http://www.cppblog.com/lambdacpp/archive/2006/05/25/7607.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lambdacpp/comments/commentRss/7607.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lambdacpp/services/trackbacks/7607.html</trackback:ping><description><![CDATA[可能是C++的设计者(BJ?)觉得没用必要引入更多的关键字，其实模板刚刚引入C++中的时候并没有typename关键字。那时候定义类模板的类型参数通常使用class关键字。如：<br />template&lt;class T&gt;<br />class Test<br />{<br />    public :<br />       T t;<br />    ..... <br />}; <br />随着模板应用的推广，大家发现使用typedef非常关键，因为实例化后的模板定义通常很长，通过使用typedef可以有效的缩短代码长度。如:<br />class UseTest<br />{<br />public:<br />    typedef Test&lt;int&gt; intTT;<br />    ...<br />};<br />这时问题就来了,当我写UseTest::intTT，这个intTT究竟是UseTest的一个静态成员(static)还是一个类型呢？所以typename关键字就引入了C++。<br />所以在定义一个intTT的对象时,我们就要这样写:<br />  typename UseTest::intTT int_tt_obj;<br />通过typename明确指出intTT是一个类型而不是一个静态成员。<br /><br /><br /><img src ="http://www.cppblog.com/lambdacpp/aggbug/7607.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lambdacpp/" target="_blank">五点半</a> 2006-05-25 01:16 <a href="http://www.cppblog.com/lambdacpp/archive/2006/05/25/7607.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>