﻿<?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++博客-我自闲庭信步,悠然自得,不亦乐乎.-随笔分类-C/C++代码</title><link>http://www.cppblog.com/huyi/category/997.html</link><description>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
------ Keep life simple&lt;br&gt;

GMail/GTalk/MSN:huyi.zg@gmail.com</description><language>zh-cn</language><lastBuildDate>Tue, 20 May 2008 02:27:28 GMT</lastBuildDate><pubDate>Tue, 20 May 2008 02:27:28 GMT</pubDate><ttl>60</ttl><item><title>将成员函数作为std::for_each的第三个参数</title><link>http://www.cppblog.com/huyi/archive/2006/12/22/16736.html</link><dc:creator>HuYi</dc:creator><author>HuYi</author><pubDate>Fri, 22 Dec 2006 07:10:00 GMT</pubDate><guid>http://www.cppblog.com/huyi/archive/2006/12/22/16736.html</guid><wfw:comment>http://www.cppblog.com/huyi/comments/16736.html</wfw:comment><comments>http://www.cppblog.com/huyi/archive/2006/12/22/16736.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/huyi/comments/commentRss/16736.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/huyi/services/trackbacks/16736.html</trackback:ping><description><![CDATA[vector&lt;Book&gt; books;<br />void CBookEditDlg::ForEachBookFunctor(Book book)<br />{<br />    ......<br />}<br />for_each(books.begin(), books.end(), std::bind1st(mem_fun(&amp;CBookEditDlg::ForEachBookFunctor), this));<br /><br />关键点在于mem_fun和bind1st的使用。<br /><br />for_each的实现中最核心的一个调用：functor(*iterater);<br />由于类非静态成员函数，必须在实例上调用：(instance-&gt;*pfn)(params);<br />所以for_each无法直接使用传过去的函数地址，函数指针的第一个参数是类的一个实例指针（this指针)，所以必须想办法把这个指针传过去（使用std::bind1st）<br /><br />关于mem_fun的一些资料，请参考<br /><a href="http://http//www.stlchina.org/documents/EffectiveSTL/files/item_41.html" target="_blank">http://www.stlchina.org/documents/EffectiveSTL/files/item_41.html</a><br /><br />对于带两个以上参数的成员函数，用stl是不能达到目的的，因为mem_fun只能生成不带参数，或者是仅带一个参数的函数对象（functor)，bind1st和bind2st也只能对第一个或者是第二个参数进行绑定。<br />要实现对任意数量参数的成员函数生成functor，必须对stl进行扩展，所幸boost已经做到了这点，boost::bind和boost::mem_fn就是更加泛化的std::bind1st和std::mem_func<br /><br />    void ForEachClassFunctor(Class c, CTreeItem treeItem)<br />    {<br />        treeView.InsertItem(c.name.c_str(), treeItem, NULL);<br />    }<br /><br />    void ForEachBookFunctor(Book book)<br />    {<br />        CTreeItem treeItem = treeView.InsertItem(book.name.c_str(), NULL, NULL);<br />        vector&lt;Class&gt; v;<br />        v.push_back(Class(0,0,"nameClass1", "titleClass1"));<br />        for_each(v.begin(), v.end(), <br />            boost::bind(boost::mem_fn(&amp;CBookEditDlg::ForEachClassFunctor), this, _1, treeItem));<br />    }<br /><img src ="http://www.cppblog.com/huyi/aggbug/16736.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/huyi/" target="_blank">HuYi</a> 2006-12-22 15:10 <a href="http://www.cppblog.com/huyi/archive/2006/12/22/16736.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>UTF-8与Unicode的相互转换</title><link>http://www.cppblog.com/huyi/archive/2006/12/22/16734.html</link><dc:creator>HuYi</dc:creator><author>HuYi</author><pubDate>Fri, 22 Dec 2006 07:09:00 GMT</pubDate><guid>http://www.cppblog.com/huyi/archive/2006/12/22/16734.html</guid><wfw:comment>http://www.cppblog.com/huyi/comments/16734.html</wfw:comment><comments>http://www.cppblog.com/huyi/archive/2006/12/22/16734.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/huyi/comments/commentRss/16734.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/huyi/services/trackbacks/16734.html</trackback:ping><description><![CDATA[今天用到了Sqlite,由于它内部是使用UTF-8编码，所以在Windows应用中出现了乱码。<br />简单的搜索了一下，相互转换的方法很多，我觉得比较好的，是<br /><a href="http://www.vckbase.com/document/viewdoc/?id=1444" target="_blank">http://www.vckbase.com/document/viewdoc/?id=1444</a><br /><br />我稍微改进了一下:<br /><br />    static WCHAR* UTF82Unicode(WCHAR* pBuffer,char *pSource, int buff_size)<br />    {<br />        int i, j, max;<br />        char* uchar = (char *)pBuffer;<br />        max = buff_size - 2;<br />        for(i = 0, j = 0; pSource[j] != '\0'; i += 2, j += 3)<br />        {<br />            if (i &gt; max) {<br />                break;<br />            }<br />            uchar[i+1] = ((pSource[j] &amp; 0x0F) &lt;&lt; 4) + ((pSource[j+1] &gt;&gt; 2) &amp; 0x0F);<br />            uchar[i] = ((pSource[j+1] &amp; 0x03) &lt;&lt; 6) + (pSource[j+2] &amp; 0x3F);<br />        }<br />        uchar[i] = '\0';<br />        uchar[i+1] = '\0';<br />        return pBuffer;<br />    }<br /><br />在Windows中的话，还有更简单的方法完成转换：<br />比如从UTF-8到Unicode:<br />    WCHAR buff[255];<br />    <span style="FONT-WEIGHT: bold">MultiByteToWideChar</span>(CP_UTF8, 0, argv[i], -1, buff, sizeof(buff));<br />    item.name = W2A(buff);<br /><br />argv[i]是要转换的字节数组<img src ="http://www.cppblog.com/huyi/aggbug/16734.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/huyi/" target="_blank">HuYi</a> 2006-12-22 15:09 <a href="http://www.cppblog.com/huyi/archive/2006/12/22/16734.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>为什么要用“((”取代“(”?</title><link>http://www.cppblog.com/huyi/archive/2006/10/13/13637.html</link><dc:creator>HuYi</dc:creator><author>HuYi</author><pubDate>Fri, 13 Oct 2006 05:50:00 GMT</pubDate><guid>http://www.cppblog.com/huyi/archive/2006/10/13/13637.html</guid><wfw:comment>http://www.cppblog.com/huyi/comments/13637.html</wfw:comment><comments>http://www.cppblog.com/huyi/archive/2006/10/13/13637.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/huyi/comments/commentRss/13637.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/huyi/services/trackbacks/13637.html</trackback:ping><description><![CDATA[
		<p>在做日志接口的时候，真实的接口函数应该是如下样式的：<br />__static_logger__.log(int level,const char* fmt, ...);<br />这里使用了printf类似的技术：可变参数。<br />这个技术可以动态的替换字符串fmt中的内容。<br />同时，这个方法可能会被重载，用于不需要可变参数的情况：<br />__static_logger__.log(int level,const char* fmt);<br /><br />通常，我们还会定义一些辅助用的宏：<br />#define KLOG(X) \<br />    do { \<br />        KDBG::printf X; \<br />    } while (0)<br /><br />使用的时候，必须按照下面的格式：<br />KLOG((LM_ERROR, "%s\n", strerror(errno)));<br />注意，使用了双层的括号“((”<br /><br />为什么不把宏改成：<br />#define KLOG(X,Y,...) \<br />    do { \<br />        KDBG::log(X,Y,__VA_ARGS__); \<br />    } while (0)s<br />从而按如下的“标准形式”来使用LOG呢？<br />KLOG(LM_ERROR, "%s\n", strerror(errno));<br /><br /><br />答案是宏不能像函数那样重载，KLOG宏只能有一个，就是最后定义的那个，也就是能接受的参数个数是固定的。<br /></p>
<img src ="http://www.cppblog.com/huyi/aggbug/13637.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/huyi/" target="_blank">HuYi</a> 2006-10-13 13:50 <a href="http://www.cppblog.com/huyi/archive/2006/10/13/13637.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>取自ACE中的bit操作宏</title><link>http://www.cppblog.com/huyi/archive/2006/04/03/4929.html</link><dc:creator>HuYi</dc:creator><author>HuYi</author><pubDate>Mon, 03 Apr 2006 02:08:00 GMT</pubDate><guid>http://www.cppblog.com/huyi/archive/2006/04/03/4929.html</guid><wfw:comment>http://www.cppblog.com/huyi/comments/4929.html</wfw:comment><comments>http://www.cppblog.com/huyi/archive/2006/04/03/4929.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/huyi/comments/commentRss/4929.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/huyi/services/trackbacks/4929.html</trackback:ping><description><![CDATA[
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
				<span style="COLOR: #000000"># define ACE_BIT_ENABLED(WORD, BIT) (((WORD) </span>
				<span style="COLOR: #000000">&amp;</span>
				<span style="COLOR: #000000"> (BIT)) </span>
				<span style="COLOR: #000000">!=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">)<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /># define ACE_BIT_DISABLED(WORD, BIT) (((WORD) </span>
				<span style="COLOR: #000000">&amp;</span>
				<span style="COLOR: #000000"> (BIT)) </span>
				<span style="COLOR: #000000">==</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">)<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /># define ACE_BIT_CMP_MASK(WORD, BIT, MASK) (((WORD) </span>
				<span style="COLOR: #000000">&amp;</span>
				<span style="COLOR: #000000"> (BIT)) </span>
				<span style="COLOR: #000000">==</span>
				<span style="COLOR: #000000"> MASK)<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /># define ACE_SET_BITS(WORD, BITS) (WORD </span>
				<span style="COLOR: #000000">|=</span>
				<span style="COLOR: #000000"> (BITS))<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /># define ACE_CLR_BITS(WORD, BITS) (WORD </span>
				<span style="COLOR: #000000">&amp;=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">~</span>
				<span style="COLOR: #000000">(BITS))</span>
		</div>
		<br />附上C中常用的位运算用法，取自《C Primer Plus》<br /><br />掩码：即mask，通常为某一个位为1，而其他位都为0的byte，如00000100。<br /><br />1.把byte中除掩码相同的位外，全部置0，掩码位不变<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #000000">flags </span><span style="COLOR: #000000">&amp;=</span><span style="COLOR: #000000"> mask</span></div>   10101111 &amp; 00000100 = 00000100<br /><br />2.打开位：打开特定的位，其他位不变。<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #000000">flags </span><span style="COLOR: #000000">|=</span><span style="COLOR: #000000"> MASK</span></div>00001000 | 00000100 = 00001100<br /><br />3.关闭位：关闭特定位，但不影响其他位<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #000000">flags </span><span style="COLOR: #000000">&amp;=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">MASK</span></div>11100101 &amp;=  ~10000000 = 01100101<br /><br />4.转置位：一个位如果打开，则关闭；如果关闭，则打开<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #000000">flags </span><span style="COLOR: #000000">^=</span><span style="COLOR: #000000"> MASK</span></div>11100101 ^=  10000000 = 01100101<br />11100101 ^=  00010000 = 11110101<br /><br />5.查看一个位的值：<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> ((flag </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> MASK) </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> MASK)<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />    do_some_work();</span></div><br /> <img src ="http://www.cppblog.com/huyi/aggbug/4929.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/huyi/" target="_blank">HuYi</a> 2006-04-03 10:08 <a href="http://www.cppblog.com/huyi/archive/2006/04/03/4929.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>给大家共享一个基本算法包</title><link>http://www.cppblog.com/huyi/archive/2006/03/23/4479.html</link><dc:creator>HuYi</dc:creator><author>HuYi</author><pubDate>Thu, 23 Mar 2006 00:51:00 GMT</pubDate><guid>http://www.cppblog.com/huyi/archive/2006/03/23/4479.html</guid><wfw:comment>http://www.cppblog.com/huyi/comments/4479.html</wfw:comment><comments>http://www.cppblog.com/huyi/archive/2006/03/23/4479.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/huyi/comments/commentRss/4479.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/huyi/services/trackbacks/4479.html</trackback:ping><description><![CDATA[
		<p>下载地址<a href="/Files/huyi/datastruct.rar">   <br />http://www.cppblog.com/Files/huyi/datastruct.rar</a><br /><br />包含内容：<br />下面是文档包含的内容:<br />二分查找1.c<br />二分查找2.c<br />二叉树.c<br />其它<br /><br />单元加<br />单循环链表.c<br />单链表.c<br />图.c<br />字符<br />定长串.c<br /><br />小写数字转为大写数字<br />带头结点双链循环线性表.c<br />底层编程<br />效验算法<br />数学问题<br />数据结构<br />数组<br />文件程序<br />求进制<br />汉诺塔<br />硬币情况<br />逆阵<br />链串.c<br />链栈.c<br />链队列.c<br />问题算法<br />顺序栈.c<br />顺序表.c<br />顺序队列.c<br /><br />./其它:<br />c语言窗体实例.zip<br />傻瓜递归.c<br />冒泡法改进.c<br />小字库DIY-.c<br />小字库DIY.c<br />小白鼠钻迷宫.c<br />扫描码.C<br />挽救软盘.c<br />汉字字模.c<br />神经元模型.c<br />穷举搜索法.c<br />简单数据库.c<br />编程汉字问题.txt<br />编随机数.c<br />试题.C<br />递堆法.C<br /><br />./单元加:<br />erre2.c<br />erre.c<br />数组完全单元.c<br />栈单元加.c<br /><br />./字符:<br />单词倒转.c<br />反出字符.c<br />回文.c<br />字符串查找.c<br />字符编辑.c<br />字符编辑技术(插入和删除) .c<br /><br />./小写数字转为大写数字:<br />小写数字转换成大写数字1.c<br />小写数字转换成大写数字2.c<br />小写数字转换成大写数字3.c<br /><br />./底层编程:<br />asm.c<br />C标志符命名源程序.c<br />ping.c<br />winsock2.c<br />时间陷阱.c<br />检出错误.c<br />检测鼠标.c<br /><br />./效验算法:<br />C.BAT<br />CMCRC.COM<br />Crctable.c<br /><br />./数学问题:<br />乘法矩阵.c<br />凉东问题<br />十五人排序.c<br />叠代整除.c<br />四分砝码.c<br />圆周率<br />多位阶乘2.c<br />多位阶乘.c<br />大加数.c<br />大小倍约.c<br />大整数.c<br />完数.c<br />小孩分糖果.c<br />小明买书<br />平方根.c<br />数学算法<br />桃子猴问题<br />灯塔问题.c<br />百鸡百钱.c<br />简单计算器.c<br />苹果纠纷<br />递推.c<br />逻辑移动.c<br />阶乘递归.c<br />阿姆斯特朗数.c<br />黑白.c<br /><br />./数学问题/凉东问题:<br />32.c<br />re.c<br />数组递归退出2.c<br />数组递归退出.c<br /><br />./数学问题/圆周率:<br />圆周率.c<br />狐狸圆周率.cpp<br /><br />./数学问题/小明买书:<br />小明买书.c<br />小明买书.cpp<br /><br />./数学问题/数学算法:<br />余弦曲线.c<br />余弦直线.c<br />符号图形.c<br />绘制圆.c<br /><br />./数学问题/桃子猴问题:<br />_notes<br />乘方函数桃子猴.c<br />桃子猴.c<br />猴子和桃.c<br />递归桃猴.c<br />题目.txt<br /><br />./数学问题/桃子猴问题/_notes:<br /><br />./数学问题/苹果纠纷:<br />ff.c<br />苹果分法.c<br /><br />./数据结构:<br />二叉排序树.c<br />二叉树实例.c<br />单链表<br />双链表正排序.c<br />各种排序法.c<br />哈夫曼算法.c<br />哈慢树.c<br />大整数.c<br />建树和遍历.c<br />排序法.c<br />推箱子.c<br />数据结构2.c<br />数据结构3.c<br />数据结构.c<br />无向图.c<br />栈操作.c<br />线性顺序存储结构.c<br />线索化二叉树.c<br />迷宫.c<br />迷宫问题.c<br />逆波兰计算器.c<br />递归车厢.c<br />队列.c<br /><br />./数据结构/单链表:<br />ww.c<br />冒泡排序.c<br />单链表1.c<br />单链表2.c<br />单链表.c<br />单链表倒序.c<br />单链表的处理全集.c<br />建立链表1.c<br />节点.c<br />质因子.c<br />链表十五人排序.c<br />链表（递归）.c<br /><br />./数组:<br />数字移动.c<br />数组操作.c<br />杨辉三角形.c<br />桶排序.c<br />矩阵转换.c<br />螺旋数组1.c<br />螺旋数组2.c<br /><br />./文件程序:<br />实例1.c<br />实例2.c<br />实例3.c<br />文件加密.c<br />文件复制.c<br />文件连接.c<br />自我复制.c<br />读写文本文件.c<br />输出自已.c<br /><br />./求进制:<br />16进制10进制.c<br />二进制数2.c<br />二进制数.c<br /><br />./汉诺塔:<br />四塔1.c<br />四塔2.c<br />换位递归.c<br />汉诺塔2.c<br />汉诺塔.c<br />诺汉塔画图版.c<br />非递归.c<br /><br />./硬币情况:<br />for循环的.c<br />硬币分法.c<br /><br />./逆阵:<br />简单逆阵.c<br />逆矩阵.c<br />逆阵.c<br /><br />./问题算法:<br />N皇后问题回溯算法.c<br />万年历<br />动态计算网络最长最短路线.c<br />矩阵乘法动态规划.c<br />网络最短路径Dijkstra算法.c<br />货郎担分枝限界图形演示.c<br />货郎担限界算法.c<br />骑士遍历<br /><br />./问题算法/万年历:<br />万年历.c<br />万年历的算法 .c<br /><br />./问题算法/骑士遍历:<br />骑士遍历1.c<br />骑士遍历2.c<br />骑士遍历回逆.c</p>
<img src ="http://www.cppblog.com/huyi/aggbug/4479.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/huyi/" target="_blank">HuYi</a> 2006-03-23 08:51 <a href="http://www.cppblog.com/huyi/archive/2006/03/23/4479.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>如何将日文汉字转换成假名显示</title><link>http://www.cppblog.com/huyi/archive/2006/03/15/4207.html</link><dc:creator>HuYi</dc:creator><author>HuYi</author><pubDate>Wed, 15 Mar 2006 09:58:00 GMT</pubDate><guid>http://www.cppblog.com/huyi/archive/2006/03/15/4207.html</guid><wfw:comment>http://www.cppblog.com/huyi/comments/4207.html</wfw:comment><comments>http://www.cppblog.com/huyi/archive/2006/03/15/4207.html#Feedback</comments><slash:comments>10</slash:comments><wfw:commentRss>http://www.cppblog.com/huyi/comments/commentRss/4207.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/huyi/services/trackbacks/4207.html</trackback:ping><description><![CDATA[<P>其实很简单啦，不过微软的API应用说明中对这个的描述也不清楚，现将我的实现代码和注意事项贴出来给大家参考。</P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080">&nbsp;1</SPAN><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">INITGUID.H</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;2</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">MSIME.h</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;3</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>.<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;4</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>.<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;5</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>.<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;6</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;CString&nbsp;sSelect;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;7</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">GetWindowText(sSelect);<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;8</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;9</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;IFELanguage&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">lang&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;NULL;<BR></SPAN><SPAN style="COLOR: #008080">10</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(SUCCEEDED(CoCreateInstance(CLSID_MSIME_JAPANESE_6,NULL,<BR></SPAN><SPAN style="COLOR: #008080">11</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CLSCTX_INPROC_SERVER,IID_IFELanguage,(</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">**</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">lang))&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;lang&nbsp;</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">&nbsp;NULL)<BR></SPAN><SPAN style="COLOR: #008080">12</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_257_310_Open_Image onclick="this.style.display='none'; Codehighlighter1_257_310_Open_Text.style.display='none'; Codehighlighter1_257_310_Closed_Image.style.display='inline'; Codehighlighter1_257_310_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_257_310_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_257_310_Closed_Text.style.display='none'; Codehighlighter1_257_310_Open_Image.style.display='inline'; Codehighlighter1_257_310_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_257_310_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cppblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_257_310_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">13</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">!</SPAN><SPAN style="COLOR: #000000">SUCCEEDED(lang</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">Open()))<BR></SPAN><SPAN style="COLOR: #008080">14</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_292_307_Open_Image onclick="this.style.display='none'; Codehighlighter1_292_307_Open_Text.style.display='none'; Codehighlighter1_292_307_Closed_Image.style.display='inline'; Codehighlighter1_292_307_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_292_307_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_292_307_Closed_Text.style.display='none'; Codehighlighter1_292_307_Open_Image.style.display='inline'; Codehighlighter1_292_307_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_292_307_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cppblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_292_307_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">15</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080">16</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">17</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">18</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(lang)<BR></SPAN><SPAN style="COLOR: #008080">19</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_323_525_Open_Image onclick="this.style.display='none'; Codehighlighter1_323_525_Open_Text.style.display='none'; Codehighlighter1_323_525_Closed_Image.style.display='inline'; Codehighlighter1_323_525_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_323_525_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_323_525_Closed_Text.style.display='none'; Codehighlighter1_323_525_Open_Image.style.display='inline'; Codehighlighter1_323_525_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_323_525_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cppblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_323_525_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">20</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CString&nbsp;sResult;<BR></SPAN><SPAN style="COLOR: #008080">21</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BSTR&nbsp;</SPAN><SPAN style="COLOR: #0000ff">out</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;NULL;<BR></SPAN><SPAN style="COLOR: #008080">22</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">(SUCCEEDED(lang</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">GetPhonetic(_bstr_t(sSelect),</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #0000ff">out</SPAN><SPAN style="COLOR: #000000">))&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">out</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">&nbsp;NULL)<BR></SPAN><SPAN style="COLOR: #008080">23</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_443_490_Open_Image onclick="this.style.display='none'; Codehighlighter1_443_490_Open_Text.style.display='none'; Codehighlighter1_443_490_Closed_Image.style.display='inline'; Codehighlighter1_443_490_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_443_490_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_443_490_Closed_Text.style.display='none'; Codehighlighter1_443_490_Open_Image.style.display='inline'; Codehighlighter1_443_490_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_443_490_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cppblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_443_490_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">24</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sResult&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">out</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080">25</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;::SysFreeString(</SPAN><SPAN style="COLOR: #0000ff">out</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">26</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">27</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">SetWindowText(sResult);<BR></SPAN><SPAN style="COLOR: #008080">28</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN></DIV>第1行的include比较重要，如果不加，容易出现链接错误，这是在platform的SDK中带有的头文件。<BR>第2行是关键性头文件，在微软japan ime文档的append部分带有，现在最新是1.3版。<BR>其他部分照猫画虎就行了，值得注意的是CLSID_MSIME_JAPANESE_6这个，如果要想在win2000下用，一定要用低版本的，推荐像我这样用6。<BR><BR>用这套API，还可以完成类似于转换拼音之类的功能，如果要创造自己的输入法，建议看看TSF，微软给了很多TSF的例子。<BR><img src ="http://www.cppblog.com/huyi/aggbug/4207.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/huyi/" target="_blank">HuYi</a> 2006-03-15 17:58 <a href="http://www.cppblog.com/huyi/archive/2006/03/15/4207.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>链接COM程序时容易出现的一个错误</title><link>http://www.cppblog.com/huyi/archive/2006/03/12/4044.html</link><dc:creator>HuYi</dc:creator><author>HuYi</author><pubDate>Sun, 12 Mar 2006 04:32:00 GMT</pubDate><guid>http://www.cppblog.com/huyi/archive/2006/03/12/4044.html</guid><wfw:comment>http://www.cppblog.com/huyi/comments/4044.html</wfw:comment><comments>http://www.cppblog.com/huyi/archive/2006/03/12/4044.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/huyi/comments/commentRss/4044.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/huyi/services/trackbacks/4044.html</trackback:ping><description><![CDATA[<P>我在链接一个程序时出现的错误信息：<BR>A Note error LNK2001: unresolved external symbol _IID_XXXXXX<BR><BR>加上如下行后链接成功。s</P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">INITGUID.H</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN></DIV><img src ="http://www.cppblog.com/huyi/aggbug/4044.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/huyi/" target="_blank">HuYi</a> 2006-03-12 12:32 <a href="http://www.cppblog.com/huyi/archive/2006/03/12/4044.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>结构体最后的长度为0或者1的数组</title><link>http://www.cppblog.com/huyi/archive/2006/03/07/3837.html</link><dc:creator>HuYi</dc:creator><author>HuYi</author><pubDate>Tue, 07 Mar 2006 04:23:00 GMT</pubDate><guid>http://www.cppblog.com/huyi/archive/2006/03/07/3837.html</guid><wfw:comment>http://www.cppblog.com/huyi/comments/3837.html</wfw:comment><comments>http://www.cppblog.com/huyi/archive/2006/03/07/3837.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/huyi/comments/commentRss/3837.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/huyi/services/trackbacks/3837.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 在Linux系统里，/usr/include/linux/if_pppox.h里面有这样一个结构：1struct&nbsp;pppoe_tag&nbsp;{2&nbsp;&nbsp;&nbsp;&nbsp;__u16&nbsp;tag_type;3&nbsp;&nbsp;&nbsp;&nbsp;__u16&nbsp;tag_len;4&nbsp;&nbsp;&nbsp;&nbsp;char&n...&nbsp;&nbsp;<a href='http://www.cppblog.com/huyi/archive/2006/03/07/3837.html'>阅读全文</a><img src ="http://www.cppblog.com/huyi/aggbug/3837.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/huyi/" target="_blank">HuYi</a> 2006-03-07 12:23 <a href="http://www.cppblog.com/huyi/archive/2006/03/07/3837.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>大写字符串快速转换成小写的函数</title><link>http://www.cppblog.com/huyi/archive/2006/03/03/3678.html</link><dc:creator>HuYi</dc:creator><author>HuYi</author><pubDate>Fri, 03 Mar 2006 05:01:00 GMT</pubDate><guid>http://www.cppblog.com/huyi/archive/2006/03/03/3678.html</guid><wfw:comment>http://www.cppblog.com/huyi/comments/3678.html</wfw:comment><comments>http://www.cppblog.com/huyi/archive/2006/03/03/3678.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/huyi/comments/commentRss/3678.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/huyi/services/trackbacks/3678.html</trackback:ping><description><![CDATA[<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080">&nbsp;1</SPAN><IMG id=Codehighlighter1_0_39_Open_Image onclick="this.style.display='none'; Codehighlighter1_0_39_Open_Text.style.display='none'; Codehighlighter1_0_39_Closed_Image.style.display='inline'; Codehighlighter1_0_39_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_0_39_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_0_39_Closed_Text.style.display='none'; Codehighlighter1_0_39_Open_Image.style.display='inline'; Codehighlighter1_0_39_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN id=Codehighlighter1_0_39_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_0_39_Open_Text><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">&nbsp;32-bit,&nbsp;little-endian.&nbsp;suif.liyuan&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;2</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;3</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;zbyter(unsigned&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;x)<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;4</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;5</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_70_328_Open_Image onclick="this.style.display='none'; Codehighlighter1_70_328_Open_Text.style.display='none'; Codehighlighter1_70_328_Closed_Image.style.display='inline'; Codehighlighter1_70_328_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_70_328_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_70_328_Closed_Text.style.display='none'; Codehighlighter1_70_328_Open_Image.style.display='inline'; Codehighlighter1_70_328_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_70_328_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cppblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_70_328_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;6</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;7</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;y;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;8</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;9</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(x&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0x7F7F7F7F</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0x7F7F7F7F</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080">10</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">11</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">~</SPAN><SPAN style="COLOR: #000000">(y&nbsp;</SPAN><SPAN style="COLOR: #000000">|</SPAN><SPAN style="COLOR: #000000">&nbsp;x&nbsp;</SPAN><SPAN style="COLOR: #000000">|</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0x7F7F7F7F</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">12</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">13</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(y&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)<BR></SPAN><SPAN style="COLOR: #008080">14</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">15</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">4</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080">16</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">17</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">else</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(y&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0x0000FFFF</SPAN><SPAN style="COLOR: #000000">)<BR></SPAN><SPAN style="COLOR: #008080">18</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">19</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;((y&nbsp;</SPAN><SPAN style="COLOR: #000000">&gt;&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">7</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #000000">^</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080">20</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">21</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">else</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">22</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">23</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;((y&nbsp;</SPAN><SPAN style="COLOR: #000000">&gt;&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">23</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #000000">^</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">3</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080">24</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">25</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">26</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">27</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;UptoLow(</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">&nbsp;pszString)<BR></SPAN><SPAN style="COLOR: #008080">28</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">29</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_362_710_Open_Image onclick="this.style.display='none'; Codehighlighter1_362_710_Open_Text.style.display='none'; Codehighlighter1_362_710_Closed_Image.style.display='inline'; Codehighlighter1_362_710_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_362_710_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_362_710_Closed_Text.style.display='none'; Codehighlighter1_362_710_Open_Image.style.display='inline'; Codehighlighter1_362_710_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_362_710_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cppblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_362_710_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">30</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">31</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">&nbsp;pByte&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080">32</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">33</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">&nbsp;pn4byte&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080">34</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">35</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;zPos;<BR></SPAN><SPAN style="COLOR: #008080">36</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">37</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pn4byte&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">)pszString;<BR></SPAN><SPAN style="COLOR: #008080">38</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">39</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">while</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">true</SPAN><SPAN style="COLOR: #000000">)<BR></SPAN><SPAN style="COLOR: #008080">40</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">41</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_485_596_Open_Image onclick="this.style.display='none'; Codehighlighter1_485_596_Open_Text.style.display='none'; Codehighlighter1_485_596_Closed_Image.style.display='inline'; Codehighlighter1_485_596_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_485_596_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_485_596_Closed_Text.style.display='none'; Codehighlighter1_485_596_Open_Image.style.display='inline'; Codehighlighter1_485_596_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_485_596_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cppblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_485_596_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">42</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">43</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zPos&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;zbyter(</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pn4byte);<BR></SPAN><SPAN style="COLOR: #008080">44</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">45</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #000000">4</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">&nbsp;zPos)&nbsp;</SPAN><SPAN style="COLOR: #0000ff">break</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080">46</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">47</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pn4byte</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">|=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0x20202020</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080">48</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">49</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">50</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">51</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pByte&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">)pn4byte;<BR></SPAN><SPAN style="COLOR: #008080">52</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">53</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;zPos;&nbsp;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">i)<BR></SPAN><SPAN style="COLOR: #008080">54</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">55</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pByte</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">|=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0x20</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080">56</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">57</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080">58</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">59</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN></DIV>似乎很高效的样子:)<img src ="http://www.cppblog.com/huyi/aggbug/3678.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/huyi/" target="_blank">HuYi</a> 2006-03-03 13:01 <a href="http://www.cppblog.com/huyi/archive/2006/03/03/3678.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>STL中string的初始化问题</title><link>http://www.cppblog.com/huyi/archive/2006/03/03/3674.html</link><dc:creator>HuYi</dc:creator><author>HuYi</author><pubDate>Fri, 03 Mar 2006 03:20:00 GMT</pubDate><guid>http://www.cppblog.com/huyi/archive/2006/03/03/3674.html</guid><wfw:comment>http://www.cppblog.com/huyi/comments/3674.html</wfw:comment><comments>http://www.cppblog.com/huyi/archive/2006/03/03/3674.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/huyi/comments/commentRss/3674.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/huyi/services/trackbacks/3674.html</trackback:ping><description><![CDATA[<P>在邮件列表中有朋友提出：</P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080">1</SPAN><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">我希望把string赋值10，然后显示</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">2</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;Buffer;<BR></SPAN><SPAN style="COLOR: #008080">3</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>sprintf(&nbsp;Buffer.begin(),</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">%d</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">10</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">4</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>MessageBox(NULL,Buffer.begin(),NULL,MB_OK);</SPAN></DIV><BR>建个程序，编译它，是通不过的。原因是Buffer没有初值<WBR>，调用Buffer.begin()返回是NULL<WBR>，第三句用MessageBox弹出一个NULL的字符串<WBR>，这是不可取的。<BR><BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080">1</SPAN><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">改正后的程序</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">2</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;Buffer&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">a</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">我随便给了个初值</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">3</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">sprintf(&nbsp;Buffer.begin(),</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">%d</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">10</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">4</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>MessageBox(NULL,Buffer.begin(),NULL,MB_OK);</SPAN></DIV><BR>但是，上面这段程序，可读性能差。别人会想：作者为什么给个"a<WBR>"而不是"b"呢？<BR><BR>所以我想请教大家：<BR>（1）这问题有没有更好的方法？最好不要MFC，也不用char数<WBR>组，因为整个程序中其它地方我都用了string。<BR>（2）STL为什么这么设计？<BR>难道定义Buffer以后，还没有分配空间？如果已经有空间<WBR>，为什么不用Begin()返回首个字符的地址？这样做意义呵在？<BR>
<P><BR>解答一：</P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080">1</SPAN><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;这么定义：</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">2</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;Buffer(</SPAN><SPAN style="COLOR: #000000">256</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">\0</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">3</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;或者</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">4</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;Buffer;<BR></SPAN><SPAN style="COLOR: #008080">5</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>Buffer.reserve(</SPAN><SPAN style="COLOR: #000000">256</SPAN><SPAN style="COLOR: #000000">);</SPAN></DIV><BR>解答二：<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080">1</SPAN><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;1.使用boost:Buffer&nbsp;=&nbsp;boost::lexical_cast&lt;string&gt;(10);<BR></SPAN><SPAN style="COLOR: #008080">2</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;2.如果不想用boost：</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">3</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">sstream</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">4</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">5</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>std::stringstream&nbsp;sstr;<BR></SPAN><SPAN style="COLOR: #008080">6</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>sstr&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">10</SPAN><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080">7</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>Buffer&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;sstr.str();<BR></SPAN><SPAN style="COLOR: #008080">8</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;或者：</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">9</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">Buffer&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(std::stringstream()&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">10</SPAN><SPAN style="COLOR: #000000">).str();&nbsp;</SPAN></DIV><BR>解答三：<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080">&nbsp;1</SPAN><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">&nbsp;&nbsp;std::vector</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;buf(buffer_size,&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;2</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;_snprintf(</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">buf[</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">],&nbsp;buffer_size,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">%d\0</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">10</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;3</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;赋值给字符串</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;4</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;std::</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;str&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">buf[</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">];<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;5</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;MessageBox(NULL,&nbsp;str.c_str(),&nbsp;NULL,&nbsp;MB_OK);<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;6</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">好处：<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;7</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;&nbsp;&nbsp;&nbsp;1.&nbsp;使用std::vector避免手动new/delete<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;8</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;&nbsp;&nbsp;&nbsp;2.&nbsp;使用stl可以充分利用stl的alloc（如stlport的memory&nbsp;pool)<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;9</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;&nbsp;&nbsp;&nbsp;3.&nbsp;使用_snprintf防止溢出<BR></SPAN><SPAN style="COLOR: #008080">10</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;&nbsp;&nbsp;&nbsp;4.&nbsp;std::string&nbsp;=&nbsp;std::vector（安逸:）</SPAN></DIV><BR>解答四：<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080">1</SPAN><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;buf[</SPAN><SPAN style="COLOR: #000000">32</SPAN><SPAN style="COLOR: #000000">];<BR></SPAN><SPAN style="COLOR: #008080">2</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;_snprintf(buf,&nbsp;</SPAN><SPAN style="COLOR: #000000">32</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">%d</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">10</SPAN><SPAN style="COLOR: #000000">);</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">或者用itoa(10,&nbsp;buf,&nbsp;10);</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">3</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;str&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;buf;</SPAN></DIV><BR>有朋友发现一个错误：<BR><FONT color=#ff0000><SPAN style="FONT-SIZE: 12pt; COLOR: navy"><FONT face=Simsun>看到一个明显错误的做法：</FONT></SPAN><FONT face=Arial><SPAN lang=EN-US style="COLOR: navy; FONT-FAMILY: Arial">str.reserve(NLEN)</SPAN></FONT><SPAN style="COLOR: navy">之后直接去</SPAN><FONT face=Arial><SPAN lang=EN-US style="COLOR: navy; FONT-FAMILY: Arial">sprintf</SPAN></FONT><SPAN style="COLOR: navy">。要知道</SPAN><FONT face=Arial><SPAN lang=EN-US style="COLOR: navy; FONT-FAMILY: Arial">reserve</SPAN></FONT><SPAN style="COLOR: navy">只是把</SPAN><FONT face=Arial><SPAN lang=EN-US style="COLOR: navy; FONT-FAMILY: Arial">capacity</SPAN></FONT><SPAN style="COLOR: navy">扩充到</SPAN><FONT face=Arial><SPAN lang=EN-US style="COLOR: navy; FONT-FAMILY: Arial">NLEN</SPAN></FONT><SPAN style="COLOR: navy">，并不会扩充</SPAN><FONT face=Arial><SPAN lang=EN-US style="COLOR: navy; FONT-FAMILY: Arial">size</SPAN></FONT><SPAN style="COLOR: navy">。对于某些标准库实现，这样的做法极端危险，它会破坏</SPAN><FONT face=Arial><SPAN lang=EN-US style="COLOR: navy; FONT-FAMILY: Arial">string</SPAN></FONT><SPAN style="COLOR: navy">的不变式，导致一系列严重问题，比如</SPAN><FONT face=Arial><SPAN lang=EN-US style="COLOR: navy; FONT-FAMILY: Arial">str.size()</SPAN></FONT><SPAN style="COLOR: navy">和</SPAN><FONT face=Arial><SPAN lang=EN-US style="COLOR: navy; FONT-FAMILY: Arial">str.length()</SPAN></FONT><SPAN style="COLOR: navy">得到错误的值。<BR><BR><A href="mailto:oscar.ken@gmail.com">oscar.ken@gmail.com</A>：<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080">&nbsp;1</SPAN><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">#ifdef&nbsp;&nbsp;UNICODE<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;2</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;3</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>#&nbsp;define&nbsp;tchar&nbsp;wchar_t<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;4</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>#&nbsp;define&nbsp;tstring&nbsp;std::wstring<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;5</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;6</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">#else</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;7</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;8</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>#&nbsp;define&nbsp;tchar&nbsp;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;9</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>#&nbsp;define&nbsp;tstring&nbsp;std::</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">10</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">11</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">#endif</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">12</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>inline&nbsp;tstring&nbsp;FormatString(</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;tchar</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">&nbsp;fmt,&nbsp;<IMG src="http://www.cppblog.com/images/dot.gif">)<BR></SPAN><SPAN style="COLOR: #008080">13</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_186_371_Open_Image onclick="this.style.display='none'; Codehighlighter1_186_371_Open_Text.style.display='none'; Codehighlighter1_186_371_Closed_Image.style.display='inline'; Codehighlighter1_186_371_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_186_371_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_186_371_Closed_Text.style.display='none'; Codehighlighter1_186_371_Open_Image.style.display='inline'; Codehighlighter1_186_371_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_186_371_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cppblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_186_371_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">14</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;tchar&nbsp;tmp[</SPAN><SPAN style="COLOR: #000000">2048</SPAN><SPAN style="COLOR: #000000">];<BR></SPAN><SPAN style="COLOR: #008080">15</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;memset(tmp,&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,&nbsp;</SPAN><SPAN style="COLOR: #000000">2048</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">sizeof</SPAN><SPAN style="COLOR: #000000">(tchar));<BR></SPAN><SPAN style="COLOR: #008080">16</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;va_list&nbsp;args;<BR></SPAN><SPAN style="COLOR: #008080">17</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;va_start(args,&nbsp;fmt);<BR></SPAN><SPAN style="COLOR: #008080">18</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;re&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;_vsntprintf(tmp,&nbsp;</SPAN><SPAN style="COLOR: #000000">2048</SPAN><SPAN style="COLOR: #000000">,&nbsp;fmt,&nbsp;args);<BR></SPAN><SPAN style="COLOR: #008080">19</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;va_end(args);<BR></SPAN><SPAN style="COLOR: #008080">20</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;tstring(tmp);<BR></SPAN><SPAN style="COLOR: #008080">21</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">22</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">23</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;如果2048不够用，可以改造一个template版本</SPAN></DIV><BR>千里马肝：<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">如果这样<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>那不如直接使用boost::format导出一个stream的string，更安逸（当然会慢一些）<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>PS，对于FormatString为了保证format出来的是一个string，每次memset，个人认为无大必要，不如自动在formatstring的最后每次自动加上\</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;(XD)</SPAN></DIV><BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">如果我没有记错的话<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>_snprintf是不会自动补\0的<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>而sprintf会<BR><IMG id=Codehighlighter1_59_61_Open_Image onclick="this.style.display='none'; Codehighlighter1_59_61_Open_Text.style.display='none'; Codehighlighter1_59_61_Closed_Image.style.display='inline'; Codehighlighter1_59_61_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_59_61_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_59_61_Closed_Text.style.display='none'; Codehighlighter1_59_61_Open_Image.style.display='inline'; Codehighlighter1_59_61_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top>所以要么char&nbsp;buf[</SPAN><SPAN style="COLOR: #000000">32</SPAN><SPAN style="COLOR: #000000">]&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_59_61_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cppblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_59_61_Open_Text><SPAN style="COLOR: #000000">{</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">}</SPAN></SPAN><SPAN style="COLOR: #000000">，要么_snprintf(<IMG src="http://www.cppblog.com/images/dot.gif">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">%d\0</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/dot.gif">)<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>另外vector的使用是为了配合“不定长buf”而存在，并非buf[</SPAN><SPAN style="COLOR: #000000">32</SPAN><SPAN style="COLOR: #000000">]的替代品</SPAN></DIV></SPAN></FONT><BR>Jiong Tu：<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">你搞错了,不补0的是strncpy,不是snprintf</SPAN></DIV><BR><PRE wrap="">吴咏炜：<BR><DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">用&nbsp;STL&nbsp;的最大好处就是空间大小可以自动增长，不会发生缓冲区溢出（安全问<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>题）。否则，又何必付出缓慢的堆上分配的代价呢？——除非要把结果传回去，那还<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>有意义；但最初苏益彧提出的问题中并不存在这样的需求。<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>另外顺便提一下，如果栈上分配内存大小不固定，也不一定就要改用堆上分配。<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>C99&nbsp;支持数组大小运行时决定（而非&nbsp;C89</SPAN><SPAN style="COLOR: #000000">/</SPAN><SPAN style="COLOR: #000000">C</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">98</SPAN><SPAN style="COLOR: #000000">&nbsp;的编译时决定），而（不支持<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>C99&nbsp;的）大部分&nbsp;C</SPAN><SPAN style="COLOR: #000000">/</SPAN><SPAN style="COLOR: #000000">C</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">&nbsp;编译器也都支持一个非标准的&nbsp;alloca&nbsp;函数用于进行栈上<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>分配。这个函数通常由编译器内联实现，需要效率的场合会用得上。<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>另外，你测出的&nbsp;</SPAN><SPAN style="COLOR: #000000">50</SPAN><SPAN style="COLOR: #000000">%</SPAN><SPAN style="COLOR: #000000">&nbsp;应当是总体的性能下降。纯粹的内存分配</SPAN><SPAN style="COLOR: #000000">/</SPAN><SPAN style="COLOR: #000000">释放操作，堆上分<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>配和栈上分配的性能根本不在同一个量级，差距通常在十倍以上。</SPAN></DIV></PRE><img src ="http://www.cppblog.com/huyi/aggbug/3674.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/huyi/" target="_blank">HuYi</a> 2006-03-03 11:20 <a href="http://www.cppblog.com/huyi/archive/2006/03/03/3674.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>