﻿<?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++博客-无法递归的五点半-随笔分类-模板元编程</title><link>http://www.cppblog.com/lambdacpp/category/2864.html</link><description>for_each(day.begin(),day.end(),bind2nd(Add(),me))</description><language>zh-cn</language><lastBuildDate>Sat, 24 May 2008 03:27:34 GMT</lastBuildDate><pubDate>Sat, 24 May 2008 03:27:34 GMT</pubDate><ttl>60</ttl><item><title>指数函数的模板元编程</title><link>http://www.cppblog.com/lambdacpp/archive/2006/10/24/14075.html</link><dc:creator>五点半</dc:creator><author>五点半</author><pubDate>Tue, 24 Oct 2006 03:09:00 GMT</pubDate><guid>http://www.cppblog.com/lambdacpp/archive/2006/10/24/14075.html</guid><wfw:comment>http://www.cppblog.com/lambdacpp/comments/14075.html</wfw:comment><comments>http://www.cppblog.com/lambdacpp/archive/2006/10/24/14075.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/lambdacpp/comments/commentRss/14075.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lambdacpp/services/trackbacks/14075.html</trackback:ping><description><![CDATA[在程序中需要将一个正整数（如123）转换成一个固定长的串（如8位，00000123）。算法有很多了。我采用可以这个方法123+10^8=100000123,将100000123转换成串"100000123"，然后返回这个串的子串substr(1)。在这个方法当中会涉及指数的运算，C++只能作浮点数的指数运算，为提高效率使用模板元编程，将这一过程提前到编译期完成。程序很简单，大家看看就明白了：<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);"><br />    template</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> d,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> pow</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);">struct</span><span style="color: rgb(0, 0, 0);"> Power<br />    {<br />        </span><span style="color: rgb(0, 0, 255);">static</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);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> value </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Power</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">d,pow</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">::value </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> d;<br />    };<br /><br />    template</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> d</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);">struct</span><span style="color: rgb(0, 0, 0);"> Power</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">d,</span><span style="color: rgb(0, 0, 0);">0</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);">static</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);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> value </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br />    };<br /><br />    </span><span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);">*<br />     * 该函数将一个整数转换成固定长度的0不齐的串，如12-&gt;"00012"<br />     </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />    template</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />    std::</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);"> int_cast_zerostr(</span><span style="color: rgb(0, 0, 255);">const</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i)<br />    {<br /><br />        std::</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);"> result  </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> boost::lexical_cast</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">std::</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);">(Power</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">,len</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">::value </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> i);<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> result.substr(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br />    }<br /><br /></span></div>如果要将12转换成"00000012"，可使用函数int_cast_zerostr&lt;8&gt;(12)。<br /><br />谢谢小明的提醒，想起Boost有个format库，所以可以这样<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);">    boost::format f(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">%08d</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />    std::</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);"> s </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (f</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">12</span><span style="color: rgb(0, 0, 0);">).str();<br />    std::cout</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">s; //s = "00000012"<br /></span></div>不过个人更喜欢int_cast_zerostr&lt;8&gt;(12)方式。呵呵！<br /><img src ="http://www.cppblog.com/lambdacpp/aggbug/14075.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-24 11:09 <a href="http://www.cppblog.com/lambdacpp/archive/2006/10/24/14075.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>