??xml version="1.0" encoding="utf-8" standalone="yes"?>久久精品国产免费,亚洲∧v久久久无码精品,九九热久久免费视频http://www.shnenglu.com/chenglong7997/category/18928.htmlzh-cnTue, 17 Jul 2012 02:08:03 GMTTue, 17 Jul 2012 02:08:03 GMT60深入理解C语言(转coolshell)http://www.shnenglu.com/chenglong7997/articles/183659.htmlSnapeSnapeSun, 15 Jul 2012 22:10:00 GMThttp://www.shnenglu.com/chenglong7997/articles/183659.htmlhttp://www.shnenglu.com/chenglong7997/comments/183659.htmlhttp://www.shnenglu.com/chenglong7997/articles/183659.html#Feedback0http://www.shnenglu.com/chenglong7997/comments/commentRss/183659.htmlhttp://www.shnenglu.com/chenglong7997/services/trackbacks/183659.html阅读全文

Snape 2012-07-16 06:10 发表评论
]]>
E序员的奋斗Ԍ一Q——浅谈几U主要编E语a(?http://www.shnenglu.com/chenglong7997/articles/170529.htmlSnapeSnapeSun, 08 Apr 2012 18:06:00 GMThttp://www.shnenglu.com/chenglong7997/articles/170529.htmlhttp://www.shnenglu.com/chenglong7997/comments/170529.htmlhttp://www.shnenglu.com/chenglong7997/articles/170529.html#Feedback0http://www.shnenglu.com/chenglong7997/comments/commentRss/170529.htmlhttp://www.shnenglu.com/chenglong7997/services/trackbacks/170529.html

E序员的奋斗Ԍ一Q?#8212;—谈几种主要~程语言

分类Q?nbsp;Others2012-04-04 15:36 8321人阅?/span> 评论(87) 收藏 举报

       l常听到有h在抱怨这个语a哪里哪里不好Q那个语a又是如何的优U。对于这L牢骚Q我只是一W而过?/span>

       我而言Q语a只是工具Q没有好坏之分。只要你采用相应的语aQ完成对应的工作Q那你的目标完成了。我们无需抱怨这U语a的缺点,而是应该抱有接受的态度。Q何语a的存在都有它的合理性,如果不合理是不会l受住时间的考验的。世间之物都h双面性,只是看利多还是弊大。如果一个东西是十全十美的,那我毫不犹U的告诉你Q一U情冉|它不存在Q另一U情冉|你还没发现它的缺炏V?/span>

       每种语言都有自己得天独厚的优势,当然~点同时伴存?span style="font-family: Calibri; ">C语言是计机界的帔R藤?span style="font-family: Calibri; ">C语言执行效率高,用于写底层的驱动那是再适合不过。指针是C语言的精华。?span style="font-family: Calibri; ">C++是很强大的,只要你是个高手,你几乎可以利?span style="font-family: Calibri; ">C++完成M事情。当然虚函数?span style="font-family: Calibri; ">C++的精华?span style="font-family: Calibri; ">Java取消了指针,q是应该喜还是忧。喜的是Q没?span style="font-family: Calibri; ">C或?span style="font-family: Calibri; ">C++那种对内存繁琐的操作Q而且也降低了很多致命性的错误发生的概率。忧的是Q效率大打折扣,采用虚拟机的方式使得Java有跨q_的特性,必然会得效率降低。另外一个方面,你借别人的东西使用Q内存)Q是不是应该q回dQ从q一斚w考虑Q?span style="font-family: Calibri; ">C或?span style="font-family: Calibri; ">C++的设计思想更有意义。经常关?span style="font-family: Calibri; ">TIOBE世界~程语言排行榜,Java已经有很久排名第一Q我觉得q样的排名只是看看而已Q无需q多的信奉。下面看看几U主要语a的优~点?/span>

 

C语言

 

C语言的优点:

1.z紧凑、灵zL?/span>

2.q算W丰?/span>

3.数据l构丰富

4. C是结构式语言

5. C语法限制不太严格Q程序设计自由度?/span>

6. C语言允许直接讉K物理地址Q可以直接对gq行操作

7. C语言E序生成代码质量高,E序执行效率?/span>

8. C语言适用范围大,可移植性好

9. C语言有一个突出的优点是适合于多U操作系l?span style="font-family: Calibri; ">,?span style="font-family: Calibri; ">DOS?span style="font-family: Calibri; ">UNIX,也适用于多U机?/span>

10.有效地将一个较复杂的程序系l设计Q务分解成许多易于控制和处理的子Q务,便于开发和l护

 

C语言的缺点:

1. C语言的缺点主要是表现在数据的装性上Q这一点?span style="font-family: Calibri; ">C在数据的安全性上做的有很大缺Pq也?span style="font-family: Calibri; ">C?span style="font-family: Calibri; ">C++的一大区别?/span>

2. C语言的语法限制不太严|对变量的cdU束不严|影响E序的安全性,Ҏl下标越界不作检查等。从应用的角度,C语言比其他高U语a较难掌握?/span>

3.可重用性差

4.数据安全性差

5.难以开发大型Y件和囑Ş界面的应用Y?/span>

6.把数据和处理数据的过E分Mؓ怺独立的实?/span>

7.当数据结构改变时Q所有相关的处理q程都要q行相应的修?/span>

8.每一U相对于老问题的新方法都要带来额外的开销

9.囑Ş用户界面的应用程序,很难用过E来描述和实玎ͼ开发和l护也都很困?/span>

 

C++

 

C++优点Q?/span>

1.可扩展性强

2.高效 z?快?/span>

3.可移植?/span>

4.面向对象的特?/span>

5.强大而灵zȝ表达能力和不输于C的效?/span>

6.支持g开?br />7.E序模块间的关系更ؓ单,E序模块的独立性、数据的安全性就有了良好的保?/span>

8.通过l承与多态性,可以大大提高E序的可重用性,使得软g的开发和l护都更为方?/span>

 

 

C++~点Q?/span>

1.比较底层Q易用性不是很?/span>

2.多重l承和友元机?/span>

3.标准库涵盖范围不?br />4.开发周期长

5.非ƈ?/span>

6.掌握有难?/span>

7._度装不够

 

Java

 

Java的优点:

1.单?/span>

2.面向对象性(面向对象的程度可以达?5%Q?/span>

3.健壮?/span>

4.跨^台?/span>

5.高性能Q自动垃圑֛收机Ӟ

6.多线E?/span>

7.动态?/span>

8.安全?/span>

 

Java的缺点:

1.效率低(毋庸|疑Q?/span>

2.跨^台是它最大的优点也是最大的~点

3.复杂性(做一个项目需要很多的知识Q涉及面q)

 

       很多时候,一U语a的优点很可能是它的缺点,~点又可能是它的优点。这主要看你是从哪方面去考虑。这印证了那句话,凡事没有必然?/span>

       不管是哪U语aQ就像文章开头所aQ只要你采用他达C惌的,那就成功了。无需q多在意它的~点Q更无需赞扬它的优点。所以网上那些关于哪U语a更好的口水仗Q我只能说这L争斗毫无意义?/span>

       语言只是工具Q没有好坏之?/span>



Snape 2012-04-09 02:06 发表评论
]]>
c间关系Q{Q?/title><link>http://www.shnenglu.com/chenglong7997/articles/169746.html</link><dc:creator>Snape</dc:creator><author>Snape</author><pubDate>Sun, 01 Apr 2012 00:15:00 GMT</pubDate><guid>http://www.shnenglu.com/chenglong7997/articles/169746.html</guid><wfw:comment>http://www.shnenglu.com/chenglong7997/comments/169746.html</wfw:comment><comments>http://www.shnenglu.com/chenglong7997/articles/169746.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/chenglong7997/comments/commentRss/169746.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/chenglong7997/services/trackbacks/169746.html</trackback:ping><description><![CDATA[<div style="font-family: arial; word-wrap: break-word; font-size: 15px; font-weight: bold; position: relative; padding-top: 32px; padding-right: 15px; padding-bottom: 8px; padding-left: 28px; margin-top: 5px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; background-image: url(http://www.cnblogs.com/Skins/gray/images/bg_post_tilte.gif); background-attachment: scroll; line-height: 18px; border-image: initial; background-repeat: no-repeat no-repeat; "><a id="cb_post_title_url" style="border-bottom-style: dotted; border-bottom-width: 0px; border-bottom-color: #efefef; color: #555555; text-decoration: none; ">c间的关p?/a></div><div class="8co4aew" id="cnblogs_post_body" style="font-family: verdana; word-wrap: break-word; font-size: 15px; line-height: 18px; "><p>|上关于此类的讨论非常多Q发现对于该问题的理解各有各的说法,而各个说法中又相ȝq。通过览q些讨论以及寏VO'Reilly - UML 2.0 In A Nutshell (2007)》的参考,发表一下自q看法</p><p>c间关系有很多种Q在大的cd上可以分ZU:U向关系、横向关pR?/p><p>U向关系是l承关系Q它的概念非常明,也成为OO的三个重要特征之一Q这里不q多的讨论?/p><p>横向关系较ؓ微妙Q按照UML的徏议大体上可以分ؓ四种Q?/p><ol style="margin-left: 2em; "><li style="list-style-type: decimal; margin-left: 10px; ">依赖    QDependencyQ?/li><li style="list-style-type: decimal; margin-left: 10px; ">兌    QAssociationQ?/li><li style="list-style-type: decimal; margin-left: 10px; ">聚合    QAggregationQ?/li><li style="list-style-type: decimal; margin-left: 10px; ">l合    QCompositionQ?/li></ol><p>它们的强弱关pL没有异议的:依赖 < 兌 < 聚合 < l合</p><p>然而它们四个之间的差别却又不那么好拿捏Q需要好好体会?/p><ol style="margin-left: 2em; "><li style="list-style-type: decimal; margin-left: 10px; "><span style="color: #0000ff; "><strong>依赖</strong></span>Q?ul style="list-style-type: disc; margin-left: 45px; "><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">UML表示法:虚线 + 头 <br /><img height="58" alt="" src="http://images.cnblogs.com/cnblogs_com/floodpeak/1.gif" width="321" border="0" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; " /></li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">关系Q? ... uses a ..."</li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">此关pL为简单,也最好理解,所谓依赖就是某个对象的功能依赖于另外的某个对象Q而被依赖的对象只是作ZU工具在使用Q而ƈ不持有对它的引用?/li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">典型的例子很多,比如Q?br />class Human<br />{<br />    public void breath()<br />    {<br />        Air freshAir = new Air();<br />        freshAir.releasePower();<br />    }<br />    public static void main()<br />    {<br />        Human me = new Human();<br />        while(true)<br />        {<br />            me.breath();<br />        }<br />    }<br />}<br /><br />class Air<br />{<br />    public void releasePower()<br />    {<br />        //do sth.<br />    }<br />}<br /> <img height="211" alt="" src="http://images.cnblogs.com/cnblogs_com/floodpeak/5.gif" width="474" border="0" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; left: 569px; top: 888px; " /></li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">释义Q一个h自创生就需要不停的呼吸Q而h的呼吸功能之所以能l持生命在于吸q来的气体发挥了作用Q所以说I气只不q是人类的一个工P而hq不持有对它的引用?/li></ul></li><li style="list-style-type: decimal; margin-left: 10px; "><span style="color: #0000ff; "><strong>兌</strong></span>Q?ul style="list-style-type: disc; margin-left: 45px; "><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">UML表示法:实线 + 头 <br /><img height="58" alt="" src="http://images.cnblogs.com/cnblogs_com/floodpeak/2.gif" width="321" border="0" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; " /></li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">关系Q? ... has a ..."</li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">所谓关联就是某个对象会长期的持有另一个对象的引用Q而二者的兌往往也是怺的。关联的两个对象彼此间没有Q何强制性的U束Q只要二者同意,可以随时解除关系或是q行兌Q它们在生命期问题上没有MU定。被兌的对象还可以再被别的对象兌Q所以关联是可以׃n的?/li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">典型的例子很多,比如Q?br />class Human<br />{<br />    ArrayList friends = new ArrayList();<br />    public void makeFriend(Human human)<br />    {<br />        friends.add(human);<br />    }<br />    public static void main()<br />    {<br />        Human me = new Human();<br />        while(true)<br />        {<br />            me.makeFriend(mySchool.getStudent());<br />        }<br />    }<br />} <br /><img height="141" alt="" src="http://images.cnblogs.com/cnblogs_com/floodpeak/6.gif" width="366" border="0" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; left: 561px; top: 1604px; " /></li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">释义Qh从生x都在不断的交朋友Q然而没有理p为朋友的生死与我的生L必然的联p,故他们的生命期没有关联,我的朋友又可以是别h的朋友,所以朋友可以共享?/li></ul></li><li style="list-style-type: decimal; margin-left: 10px; "><span style="color: #0000ff; "><strong>聚合</strong></span>Q?nbsp; <ul style="list-style-type: disc; margin-left: 45px; "><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">UML表示法:I心菱Ş + 实线 + 头 <br /><img height="58" alt="" src="http://images.cnblogs.com/cnblogs_com/floodpeak/3.gif" width="321" border="0" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; " /></li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">关系Q? ... owns a ..."</li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">聚合是强版本的关联。它暗含着一U所属关pM及生命期关系。被聚合的对象还可以再被别的对象兌Q所以被聚合对象是可以共享的。虽然是׃n的,聚合代表的是一U更亲密的关pR?/li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">典型的例子很多,比如Q?br />class Human<br />{<br />    Home myHome;<br />    public void goHome()<br />    {<br />        //在回家的路上<br />        myHome.openDoor();<br />        //看电?br />    }<br />    public static void main()<br />    {<br />        Human me = new Human();<br />        while(true)<br />        {<br />            //上学<br />            //吃饭<br />            me.goHome();<br />        }<br />    }<br />}<br /> <img height="221" alt="" src="http://images.cnblogs.com/cnblogs_com/floodpeak/7.gif" width="566" border="0" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; left: 477px; top: 2185px; " /></li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">释义Q我的家和我之间h着一U强烈的所属关p,我的家是可以分n的,而这里的分n又可以有两种。其一是聚合间的分享,q正如你和你媛_儉K对这个家有着同样的强烈关联;其二是聚合与兌的分享,如果你的朋友来家里吃个便饭,估计你不会给他配一把钥匙?/li></ul></li><li style="list-style-type: decimal; margin-left: 10px; "><span style="color: #0000ff; "><strong>l合</strong></span>Q?ul style="list-style-type: disc; margin-left: 45px; "><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">UML表示法:实心菱Ş + 实线 + 头 <br /><img height="58" alt="" src="http://images.cnblogs.com/cnblogs_com/floodpeak/4.gif" width="321" border="0" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; " /></li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">关系Q? ... is a part of  ..."</li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">l合是关pd中的最强版本,它直接要求包含对象对被包含对象的拥有以及包含对象与被包含对象生命期的关系。被包含的对象还可以再被别的对象兌Q所以被包含对象是可以共享的Q然而绝不存在两个包含对象对同一个被包含对象的共享?/li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">典型的例子很多,比如Q?br />class Human<br />{<br />    Heart myHeart = new Heart();<br />    public static void main()<br />    {<br />        Human me = new Human();<br />        while(true)<br />        {<br />            myHeart.beat();<br />        }<br />    }<br />}<br /><img height="215" alt="" src="http://images.cnblogs.com/cnblogs_com/floodpeak/8.gif" width="578" border="0" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; left: 321px; top: 2728px; " /></li><li style="list-style-type: decimal; margin-left: 10px; list-style: inherit; ">释义Q组合关pd是整体与部分的关p,部分属于整体Q整体不存在Q部分一定不存在Q然而部分不存在整体是可以存在的Q说的更明确一些就是部分必d生于整体创生之后Q而销毁于整体销毁之前。部分在q个生命期内可以被其它对象关联甚臌合,但有一点必L意,一旦部分所属于的整体销毁了Q那么与之关联的对象中的引用׃成ؓI引用,q一点可以利用程序来保障。心脏的生命期与人的生命期是一致的Q如果换个部分就不那么一定,比如阑尾Q很多h在创生后的某个时间对其厌倦便提前销毁了它,可它和hcȝ关系不可辩驳的属于组合?br />在UML中存在一U特例,是允许被包含对象在包含对象销毁前转移l新的对象,q虽然不自然Q但它给需要心脏移植的患者带来了音?/li></ul></li></ol></div><img src ="http://www.shnenglu.com/chenglong7997/aggbug/169746.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/chenglong7997/" target="_blank">Snape</a> 2012-04-01 08:15 <a href="http://www.shnenglu.com/chenglong7997/articles/169746.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>教你文本聚类Q{Q?/title><link>http://www.shnenglu.com/chenglong7997/articles/169741.html</link><dc:creator>Snape</dc:creator><author>Snape</author><pubDate>Sat, 31 Mar 2012 23:57:00 GMT</pubDate><guid>http://www.shnenglu.com/chenglong7997/articles/169741.html</guid><wfw:comment>http://www.shnenglu.com/chenglong7997/comments/169741.html</wfw:comment><comments>http://www.shnenglu.com/chenglong7997/articles/169741.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/chenglong7997/comments/commentRss/169741.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/chenglong7997/services/trackbacks/169741.html</trackback:ping><description><![CDATA[     摘要: 教你文本聚类2009-08-23 18:32 189人阅?nbsp;评论(0) 收藏 举报?要:文本聚类是搜索引擎和语义web的基本技术,q次本蛙和大家一起学习一下简单的文本聚类法Q可能不能直接用于实际应用中Q但对于惛_搜烦技术的初学 者还是有一定入门作用的。这里会用到TF/IDF权重Q用余u夹角计算文本怼度,用方差计两个数据间Ƨ式距离Q用k-meansq行...  <a href='http://www.shnenglu.com/chenglong7997/articles/169741.html'>阅读全文</a><img src ="http://www.shnenglu.com/chenglong7997/aggbug/169741.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/chenglong7997/" target="_blank">Snape</a> 2012-04-01 07:57 <a href="http://www.shnenglu.com/chenglong7997/articles/169741.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>字符串相似度法介绍(整理)(?http://www.shnenglu.com/chenglong7997/articles/169740.htmlSnapeSnapeSat, 31 Mar 2012 23:52:00 GMThttp://www.shnenglu.com/chenglong7997/articles/169740.htmlhttp://www.shnenglu.com/chenglong7997/comments/169740.htmlhttp://www.shnenglu.com/chenglong7997/articles/169740.html#Feedback0http://www.shnenglu.com/chenglong7997/comments/commentRss/169740.htmlhttp://www.shnenglu.com/chenglong7997/services/trackbacks/169740.html最q在做这斚w的应用,把我扑ֈ的资料脓出来Q有需要的人可以参考参考?br />1Q编辑距(Levenshtein DistanceQ?br />~辑距离是用来计算从原ԌsQ{换到目标?t)所需要的最的插入Q删除和替换
的数目,在NLP中应用比较广泛,如一些评方法中qCQwer,mWer{)Q同时也常用来计你对原文本所作的改动数。编辑距ȝ法是首先由俄国U学家Levenshtein提出的,故又叫Levenshtein Distance?br />Levenshtein Distance法可以看作动态规划。它的思\是从两个字W串的左边开始比?记录已经比较q的子串怼?实际上叫做距?,然后q一步得C一?字符位置时的怼度?用下面的例子: GUMBO和GAMBOL。当到矩阵D[3,3]位置?也就是当比较到GUM和GAM?要从已经比较q的3对子串GU-GAM, GUM-GA和GU-GA之中选一个差别最的来当它的? 所以要从左上到右下构造矩c?br />~辑距离的伪法Q?br />整数 Levenshtein距离(字符 str1[1..lenStr1], 字符 str2[1..lenStr2])
   宣告 int d[0..lenStr1, 0..lenStr2]
   宣告 int i, j, cost
 
   对于 i {于 ?0 ?lenStr1
       d[i, 0] := i
   对于 j {于 ?0 ?lenStr2
       d[0, j] := j
   对于 i {于 ?1 ?lenStr1
       对于 j {于 ?1 ?lenStr2
           ?str1[i] = str2[j] ?cost := 0
                                否则 cost := 1
           d[i, j] := 最?
                                d[i-1, j  ] + 1,     // 删除
                                d[i  , j-1] + 1,     // 插入
                                d[i-1, j-1] + cost   // 替换
                            )
q回 d[lenStr1, lenStr2]

double Minimum(double a, double b, double c) 

 double mi; 
 
 mi = a; 
 if (b < mi) { 
  mi = b; 
 } 
 if (c < mi) { 
  mi = c; 
 } 
 return mi; 
}


int* GetCellPointer(int *pOrigin, int col, int row, int nCols) 

 return pOrigin + col + (row * (nCols + 1)); 
}

int GetAt(int *pOrigin, int col, int row, int nCols) 

 int *pCell; 
 
 pCell = GetCellPointer (pOrigin, col, row, nCols); 
 return *pCell; 
}

void PutAt(int *pOrigin, int col, int row, int nCols, double x) 

 int *pCell; 
 pCell = GetCellPointer (pOrigin, col, row, nCols); 
 *pCell = x; 
}

//~辑距离
LD(const char *s, const char *t)
{
 int *d; // pointer to matrix
 int n; // length of s
 int m; // length of t
 int i; // iterates through s
 int j; // iterates through t
 char s_i1; // ith character of s
 char s_i2; // ith character of s
 char t_j1; // jth character of t
 char t_j2; // jth character of t
 int *cost; // cost代h矩阵
 int result; // result
 int cell; // contents of target cell
 int above; // contents of cell immediately above
 int left; // contents of cell immediately to left
 int diag; // contents of cell immediately above and to left
 int sz; // number of cells in matrix

 // Step 1

 n = strlen (s);
 m = strlen (t);
 if (n == 0) 
 {
  return m;
 }
 if (m == 0) 
 {
  return n;
 }
 sz = (n+1) * (m+1) * sizeof (int);
 d = (int *) malloc (sz);
 cost = (int *) malloc (sz);

 // Step 2

 for (i = 0; i <= n; i++) 
 {
  PutAt (d, i, 0, n, i);
 }

 for (j = 0; j <= m; j++)
 {
  PutAt (d, 0, j, n, j);
 }
 for (int g=0;g<=m;g++)//把代仯ȝ阵全部初始化为同一个|以后可根据此值判断相应的Ҏ是否被赋q?br /> {
  for(int h=0;h<=n;h++)
  {
   PutAt(cost,h,g,n,2);
  }
 }
 // Step 3

 for (i = 1; i <= n; i++) 
 {

  s_i1 = s[i-1];
  s_i2 = s[i];
  bool sbd=false;
  bool tbd=false;
  if(s_i1>=' '&&s_i1<='@'||s_i1>='A'&&s_i1<='~' )
  {//s为标点符h其他非中文符号和数字
  sbd=true;
  }
  // Step 4

  for (j = 1; j <= m; j++) 
  {

   tbd=false;
   t_j1 = t[j-1];
   t_j2 = t[j];
   // Step 5
   if(t_j1>=' '&&t_j1<='@'||t_j1>='A'&&t_j1<='~' )
   {//t也ؓ标点W号
    tbd=true;
   }
   if(!sbd)
   {//s为汉?br />    if(!tbd)
    {//t也ؓ汉字
     if (s_i1 == t_j1&&s_i2 == t_j2) 
     {
      bool tt=false;
      int temp=GetAt(cost,i,j,n);
      if(temp==2)
      {
       PutAt(cost,i,j,n,0);
       tt=true;
      }
      if(tt)
      {//因ؓst全市汉字Q所以把代h矩阵他相ȝ未赋q值的三个D?br />       int temp1=GetAt(cost,i+1,j,n);
       if(temp1==2)
       {
        PutAt(cost,i+1,j,n,0);
       }
       int temp2=GetAt(cost,i,j+1,n);
       if(temp2==2)
       {
        PutAt(cost,i,j+1,n,0);
       }
       int temp3=GetAt(cost,i+1,j+1,n);
       if(temp3==2)
       {
        PutAt(cost,i+1,j+1,n,0);
       }
      }
     }
     else 
     {
      bool tt=false;
      int temp=GetAt(cost,i,j,n);
      if(temp==2)
      {
       PutAt(cost,i,j,n,1);
       tt=true;
      }
      if(tt)
      {
       int temp1=GetAt(cost,i+1,j,n);
       if(temp1==2)
       {
        PutAt(cost,i+1,j,n,1);
       }
       int temp2=GetAt(cost,i,j+1,n);
       if(temp2==2)
       {
        PutAt(cost,i,j+1,n,1);
       }
       int temp3=GetAt(cost,i+1,j+1,n);
       if(temp3==2)
       {
        PutAt(cost,i+1,j+1,n,1);
       }
      }
     }
    }
    else
    {//t为符?br />     bool tt=false;
     int temp=GetAt(cost,i,j,n);
     if(temp==2)
     {
      PutAt(cost,i,j,n,1);
      tt=true;
     }
     if(tt)
     {
      int temp1=GetAt(cost,i+1,j,n);
      if(temp1==2)
      {
       PutAt(cost,i+1,j,n,1);
      }
     }
    
    }

   }
   else
   {//s为符?br />    if(!tbd)
    {//t为汉?nbsp;
     bool tt=false;
     int temp=GetAt(cost,i,j,n);
     if(temp==2)
     {
      PutAt(cost,i,j,n,1);
      tt=true;
     }
     if(tt)
     {
      int temp1=GetAt(cost,i,j+1,n);
      if(temp1==2)
      {
       PutAt(cost,i,j+1,n,1);
      }
     }
    }
    else
    {
     if(s_i1==t_j1)
     {
      int temp=GetAt(cost,i,j,n);
      if(temp==2)
      {
       PutAt(cost,i,j,n,0);
      }
     }
     else
     {
      int temp=GetAt(cost,i,j,n);
      if(temp==2)
      {
       PutAt(cost,i,j,n,1);
      }
     }
    }

   }

    // Step 6

   above = GetAt (d,i-1,j, n);
   left = GetAt (d,i, j-1, n);
   diag = GetAt (d, i-1,j-1, n);
   int curcost=GetAt(cost,i,j,n);
   cell = Minimum (above + 1, left + 1, diag + curcost);
   PutAt (d, i, j, n, cell);
  }
 }

  // Step 7

  result = GetAt (d, n, m, n);
  free (d);
  return result;

}

2Q最长公共子?(LCS)
LCS问题是求两个字W串最长公共子串的问题。解法就是用一个矩阉|记录两个字符
串中所有位|的两个字符之间的匹配情况,若是匚w则ؓ1Q否则ؓ0。然后求出对角线最长的1序列Q其对应的位|就是最长匹配子串的位置.
下面是字W串21232523311324和字W串312123223445的匹配矩阵,前者ؓX方向的,
后者ؓY方向的。不难找刎ͼU色部分是最长的匚w子串。通过查找位置我们得到最长的匚w子串为:21232
    0 0 0 1 0 0 0 1 1 0 0 1 0 0 0
  0 1 0 0 0 0 0 0 0 1 1 0 0 0 0
  1 0 1 0 1 0 1 0 0 0 0 0 1 0 0
  0 1 0 0 0 0 0 0 0 1 1 0 0 0 0
  1 0 1 0 1 0 1 0 0 0 0 0 1 0 0
  0 0 0 1 0 0 0 1 1 0 0 1 0 0 0
  1 0 1 0 1 0 1 0 0 0 0 0 1 0 0
  1 0 1 0 1 0 1 0 0 0 0 0 1 0 0
  0 0 0 1 0 0 0 1 1 0 0 1 0 0 0
  0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
  0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
  0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 
  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
但是??的矩阵中找最长的1对角U序列又要花M定的旉。通过改进矩阵的生成方式和讄标记变量Q可以省去这部分旉。下面是新的矩阵生成方式Q?br />    0 0 0 1 0 0 0 1 1 0 0 1 0 0 0
  0 1 0 0 0 0 0 0 0 2 1 0 0 0 0
  1 0 2 0 1 0 1 0 0 0 0 0 1 0 0
  0 2 0 0 0 0 0 0 0 1 1 0 0 0 0
  1 0 3 0 1 0 1 0 0 0 0 0 1 0 0
  0 0 0 4 0 0 0 2 1 0 0 1 0 0 0
  1 0 1 0 5 0 1 0 0 0 0 0 2 0 0
  1 0 1 0 1 0 1 0 0 0 0 0 1 0 0
  0 0 0 2 0 0 0 2 1 0 0 1 0 0 0
  0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
  0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
  0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
?字符匚w的时候,我们q不是简单的l相应元素赋?Q而是赋上其左上角元素的值加一。我们用两个标记变量来标记矩阵中值最大的元素的位|,在矩는成的q?E中来判断当前生成的元素的值是不是最大的Q据此来改变标记变量的|那么到矩阵完成的时候,最长匹配子串的位置和长度就已经出来了?/p>

//最长公共子?br />char* LCS(char*left,char* right){
 int lenLeft,lenRight;
 lenLeft = strlen(left);
 lenRight = strlen(right);
 int *c = new int[lenRight];
 int start,end,len;
 end = len = 0;
 for(int i = 0; i < lenLeft; i++){
  for(int j = lenRight-1; j >= 0; j--){
   if(left[i] == right[j]){
    if(i == 0 || j == 0)
     c[j] = 1;
    else
     c[j] = c[j-1]+1;
   }
   else
    c[j] = 0;
   if(c[j] > len){
    len = c[j];
    end = j;
   }
  }
 }
 char *p = new char[len+1];
 start = end - len + 1;
 for(i = start; i <= end; i++)
  p[i - start] = right[i];
 p[len] = '/0';
 return p;
}
3. 余u定理 (向量I间法)
余u定理古老而广泛的数学概念Q在各个学科及实践中都得C大量的应用,q里单的介绍下其在判断两个字W串怼度的应用?br />在余弦定理中基本的公式ؓQ?/p>

假如字符串s1与s2Q比较两个字W串的相似度Qsim(s1,s2)Q假设s1,s2中含有n个不同的字符Q其分别为c1,c2,...cnQ判 断字W串的相似度转换Z个字W串对应的向量v1,v2之间夹角大小的判断,余uD大其向量之间的夹角越,s1与S2的相似度大?br />向量I间法的介l:
?向量I间模型中,文本泛指各种机器可读的记录。用DQDocumentQ表C,特征(TermQ用t表示Q是指出现在文档D中且能够代表该文档内容的?本语a单位Q主要是p或者短语构成,文本可以用特征项集表CZؓD(T1QT2Q?#8230;QTn)Q其中Tk是特征项Q?<=k<=N。例如一文 档中有a、b、c、d四个特征,那么q篇文档可以表CZؓD(aQbQcQd)。对含有n个特征项的文本而言Q通常会给每个特征赋予一定的权重表示?重要E度。即DQD(T1QW1QT2QW2Q?#8230;QTnQWn)Q简CؓDQD(W1QW2Q?#8230;QWn)Q我们把它叫做文本D的向量表C。其中Wk是Tk 的权重,1<=k<=N。在上面那个例子中,假设a、b、c、d的权重分别ؓ30Q?0Q?0Q?0Q那么该文本的向量表CZؓ D(30Q?0Q?0Q?0)。在向量I间模型中,两个文本D1和D2之间的内容相兛_Sim(D1QD2)常用向量之间夹角的余弦DC,公式为:  

 
其中QW1k、W2k分别表示文本D1和D2WK个特征项的权|1<=k<=N。我们可以利用类似的Ҏ来计两个字W串的相兛_?nbsp;   
q个法|上没找刎ͼ虽然我写q,但是没什么通用性,׃贴出来。很单的Q有兴趣的可以自己写一个?/p>

Snape 2012-04-01 07:52 发表评论
]]>
【{】面向对象设计中的经验原?/title><link>http://www.shnenglu.com/chenglong7997/articles/169739.html</link><dc:creator>Snape</dc:creator><author>Snape</author><pubDate>Sat, 31 Mar 2012 23:41:00 GMT</pubDate><guid>http://www.shnenglu.com/chenglong7997/articles/169739.html</guid><wfw:comment>http://www.shnenglu.com/chenglong7997/comments/169739.html</wfw:comment><comments>http://www.shnenglu.com/chenglong7997/articles/169739.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/chenglong7997/comments/commentRss/169739.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/chenglong7997/services/trackbacks/169739.html</trackback:ping><description><![CDATA[<h1><a id="cb_post_title_url" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #6466b3; text-decoration: none; ">【{】面向对象设计中的经验原?/a></h1><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; clear: both; color: #464646; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; text-align: left; background-color: #faf7ef; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 5px; padding-right: 2px; padding-bottom: 5px; padding-left: 5px; line-height: 1.5; color: #393939; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: black; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; text-align: left; background-color: #faf7ef; "><div class="8i84oy4" id="cnblogs_post_body" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(1)所有数据都应该隐藏在所在的cȝ内部?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(2)cȝ使用者必M赖类的共有接口,但类不能依赖它的使用者?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(3)量减少cȝ协议中的消息?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(4)实现所有类都理解的最基本公有接口[例如Q拷贝操?深拷贝和拷?、相{性判断、正输出内宏V从ASCII描述解析{等]?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(5)不要把实现细?例如攄q代码的私有函?攑ֈcȝ公有接口中。如果类的两个方法有一D公׃码,那么可以创Z个防止这些公׃码的U有函数?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(6)不要以用h法用或不感兴趣的东西扰q的公有接口?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(7)cM间应该零耦合Q或者只有导合关系。也卻I一个类要么同另一个类毫无关系Q要么只使用另一个类的公有接口中的操作?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(8)cd该只表示一个关键抽象。包中的所有类对于同一cL质的变化应该是共同闭的。一个变化若对一个包影响Q则对包中的所有类产生影响Q而对其他的包不造成M影响?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(9)把相关的数据和行为集中放|。设计者应当留意那些通过get之类操作从别的对象中获取数据的对象。这U类型的行ؓ暗示着q条l验原则被违反了?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(10)把不相关的信息放在另一个类?也即Q互不沟通的行ؓ)。朝着E_的方向进行依赖?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(11)保你ؓ之徏模的抽象概念是类Q而不只是对象扮演的角艌Ӏ?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(12)在水qx向上可能统一地分布系l功能,也即Q按照设计,层cd当统一地共享工作?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(13)在你的系l中不要创徏全能c?对象。对名字包含Driver、Manager、System、Susystem的类要特别多加小心。规划一个接口而不是实C个接口?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(14)对公共接口中定义了大量访问方法的cd加小心。大量访问方法意味着相关数据和行为没有集中存放?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(15)对包含太多互不沟通的行ؓ的类多加心。这个问题的另一表现是在你的应用E序中的cȝ公有接口中创Z很多的get和set函数?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(16)在由同用L面交互的面向对象模型构成的应用程序中Q模型不应该依赖于界面,界面则应当依赖于模型?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(17)可能地按照现实世界建模(我们常常Z遵守pȝ功能分布原则、避免全能类原则以及集中攄相关数据和行为的原则而违背这条原? ?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(18)从你的设计中去除不需要的cR一般来_我们会把q个c降U成一个属性?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(19)去除pȝ外的cR系l外的类的特ҎQ抽象地看它们只往pȝ领域发送消息但q不接受pȝ领域内其他类发出的消息?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(20)不要把操作变成类。质疑Q何名字是动词或者派生自动词的类Q特别是只有一个有意义行ؓ的类。考虑一下那个有意义的行为是否应当迁Ud已经存在或者尚未发现的某个cM?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(21)我们在创建应用程序的分析模型时常常引入代理类。在设计阶段Q我们常会发现很多代理没有用的,应当去除?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(22)量减少cȝ协作者的数量。一个类用到的其他类的数目应当尽量少?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(23)量减少cd协作者之间传递的消息的数量?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(24)量减少cd协作者之间的协作量,也即Q减类和协作者之间传递的不同消息的数量?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(25)量减少cȝ扇出Q也卻I减少cd义的消息数和发送的消息数的乘积?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(26)如果cd含另一个类的对象,那么包含cd当给被包含的对象发送消息。也卻I包含关系L意味着使用关系?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(27)cM定义的大多数Ҏ都应当在大多数时间里使用大多数数据成员?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(28)cd含的对象数目不应当超q开发者短期记忆的定w。这个数目常常是6。当cd含多?个数据成员时Q可以把逻辑相关的数据成员划分ؓ一l,然后用一个新的包含类d含这一l成员?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(29)让系l功能在H而深的承体pM垂直分布?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(30)在实现语义约束时Q最好根据类定义来实现。这常常会导致类泛滥成灾Q在q种情况下,U束应当在类的行Z实现Q通常是在构造函C实现Q但不是必须如此?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(31)在类的构造函C实现语义U束Ӟ把约束测试放在构造函数领域所允许的尽量深的包含层ơ中?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(32)U束所依赖的语义信息如果经常改变,那么最好放在一个集中式的第3方对象中?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(33)U束所依赖的语义信息如果很改变,那么最好分布在U束所涉及的各个类中?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(34)cdȝ道它包含什么,但是不能知道谁包含它?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(35)׃n字面范围(也就是被同一个类所包含)的对象相互之间不应当有用关pR?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(36)l承只应被用来ؓ特化层次l构建模?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(37)zcdȝ道基c,基类不应该知道关于它们的zcȝM信息?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(38)基类中的所有数据都应当是私有的Q不要用保护数据。类的设计者永q都不应该把cȝ使用者不需要的东西攑֜公有接口中?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(39)在理ZQ承层ơ体pd当深一点,深好?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(40)在实践中Q承层ơ体pȝ深度不应当超Z个普通h的短期记忆能力。一个广为接受的深度值是6?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(41)所有的抽象c都应当是基cR?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(42)所有的基类都应当是抽象cR?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(43)把数据、行为和/或接口的共性尽可能地放到承层ơ体pȝ高端?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(44)如果两个或更多个cd享公共数?但没有公p?Q那么应当把公共数据攑֜一个类中,每个׃nq个数据的类都包含这个类?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(45)如果两个或更多个cL共同的数据和行ؓ(是Ҏ)Q那么这些类的每一个都应当从一个表CZq些数据和方法的公共基类l承?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(46)如果两个或更多个cd享公共接?指的是消息,而不是方?Q那么只有他们需要被多态地使用Ӟ他们才应当从一个公共基cȝѝ?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(47)对对象类型的昄的分情况分析一般是错误的。在大多数这L情况下,设计者应当用多态?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(48)对属性值的昄的分情况分析常常是错误的。类应当解耦合成一个承层ơ结构,每个属性值都被变换成一个派生类?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(49)不要通过l承关系来ؓcȝ动态语义徏模。试囄静态语义关pL为动态语义徏模会D在运行时切换cd?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(50)不要把类的对象变成派生类。对M只有一个实例的zc都要多加小心?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(51)如果你觉得需要在q行时刻创徏新的c,那么退后一步以认清你要创徏的是对象。现在,把这些对象概括成一个类?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(52)在派生类中用I方?也就是什么也不做的方?来覆写基cM的方法应当是非法的?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(53)不要把可选包含同对承的需要相h。把可选包含徏模成l承会带来泛滥成灄cR?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(54)在创建承层ơ时Q试着创徏可复用的框架Q而不是可复用的组件?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(55)如果你在设计中用了多重l承Q先假设你犯了错误。如果没犯错误,你需要设法证明?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(56)只要在面向对象设计中用到了承,问自׃个问题:(1)zcL否是它承的那个东西的一个特D类型?(2)基类是不是派生类的一部分Q?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(57)如果你在一个面向对象设计中发现了多重承关p,保没有哪个基类实际上是另一个基cȝzcR?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(58)在面向对象设计中如果你需要在包含关系和关联关p间作出选择Q请选择包含关系?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(59)不要把全局数据或全局函数用于cȝ对象的薄记工作。应当用类变量或类Ҏ?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(60)面向对象设计者不应当让物理设计准则来破坏他们的逻辑设计。但是,在对逻辑设计作出决策的过E中我们l常用到物理设计准则?/span></p><p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 10pt; ">(61)不要l开公共接口M改对象的状态?/span></p></div></div><img src ="http://www.shnenglu.com/chenglong7997/aggbug/169739.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/chenglong7997/" target="_blank">Snape</a> 2012-04-01 07:41 <a href="http://www.shnenglu.com/chenglong7997/articles/169739.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于默认构造函数的几个错误认识Q四U情况下Q编译器会生成默认构造函敎ͼhttp://www.shnenglu.com/chenglong7997/articles/169731.htmlSnapeSnapeSat, 31 Mar 2012 22:19:00 GMThttp://www.shnenglu.com/chenglong7997/articles/169731.htmlhttp://www.shnenglu.com/chenglong7997/comments/169731.htmlhttp://www.shnenglu.com/chenglong7997/articles/169731.html#Feedback0http://www.shnenglu.com/chenglong7997/comments/commentRss/169731.htmlhttp://www.shnenglu.com/chenglong7997/services/trackbacks/169731.html
 假期间闲来无?׃载了某大师的VC++视频资料。在讲到C++?说是如果E序员没?span style="color: red; ">自己定义默认构造函?/span>,那么~译器会自动为我?span style="color: red; ">产生一个默认的构造函?/span>?本来q个错误的认识很多程序员都有,不为奇。但有这么多q编E经验的高手也有q样的错误认识就不禁让我哑然了?/div>
        其实~程语言和我们所用的M软g没有区别,例如Photoshop、AutoCAD之类。其唯一不同的是我们用的~程语言是基于编译器?而应用Y件是Z我们的编E语a的?/div>
        既然我们所用的软g是基于编译器?那么理解~译器在背后到底为我们做了些什么、在什么情况下做了哪些事情显得异帔R要。这像Photoshop会ؓ你生一些基本图形例如矩形、三角Ş之类,而不会凭IZ生一些风景优的囄一栗?/div>
        在《C++ Annotated Reference Manual(ARM)[ELLIS90]》中的Section 12.1告诉我们:"Default constructors...在需要的时候被~译器生出??/div>
        其实默认构造函C是分Zcȝ:有用的、无用的?/div>
        所?span style="color: red; ">有用的标准也是就默认构造函C为我们的cd一些初始化操作。那么无用的׃会做M工作,从而对我们的类也就没有M意义。所?我们通常所说的默认构造函数是?span style="color: red; ">有用的默认构造函?其英文名字叫nontrivial default constructor?/span>
        那么到底什么时候编译器会ؓ我们产生nontrivial default constructor?有下面四中情?
       ①如果一个类里面某个成员对象有nontrivial default constructor,~译器就会ؓ我们的类产生nontrivial default constructor?/span>
       那么~译器这样做的理由是什?
       {案是因为类成员对象有nontrivial default constructor,那么~译器就需?span style="color: red; ">昑ּ的来调用q个cL员对象的nontrivial default constructor。而编译器x式的调用cL员对象的nontrivial default constructor,需要自己来合成一些代码来调用。但是记?~译器合成的nontrivial default constructor仅仅调用cL员对象的默认构造函?而不Ҏ们类里面的其它变量做M初始化操作?/span>
        也就是说,如果你想初始化类成员变量以外的变量例如一个int、一个String,那么?span style="color: red; ">自己定义默认构造函数来完成q些变量的初始化。而编译器会对你定义的默认构造函数做相应的扩?从而调用类成员对象的nontrivial default constructor?/div>
        ②如果一个派生类?span style="color: red; ">基类?/span>nontrivial default constructor,那么~译器会为派生类合成一个nontrivial default constructor?/div>
        ~译器这L理由?因ؓzc被合成旉要显式调用基cȝ默认构造函数?/div>
        ③如何一个类里面隐式的含?span style="color: red; ">Mvirtual function table(或vtbl)、pointer member(或vptr)?/span>
        ~译器这样做的理由很?因ؓq些vtbl或vptr需要编译器隐式(implicit)的合成出?那么~译器就把合成动作放C默认构造函数里面。所以编译器必须自己产生一个默认构造函数来完成q些操作?/div>
        所以如果你的类里带有Q?span style="color: red; ">virtual function,那么~译器会Z合成一个默认构造函数?/span>
        ④如果一?span style="color: red; ">c虚l承于其它类?/span>
        ~译器这样做的理由和③cM:因ؓ虚扉K要维护一个类似指针一?可以动态的军_内存地址的东?不同~译器对虚承的实现不仅相同)?/div>
        那么除了以上四种情况,~译器ƈ不会为我们的cM生默认构造函数?/span>
        所以编E中切忌惛_?要明白哪些事情是~译器做?哪些事情需要程序员来完成的。就像堆所占用的资源需要程序员自己来释?而栈I间是编译器理的一栗?/div>
        只有如此,才能~写量更高的代码?/div>



Snape 2012-04-01 06:19 发表评论
]]>如何学好C++语言Q{Q?/title><link>http://www.shnenglu.com/chenglong7997/articles/169730.html</link><dc:creator>Snape</dc:creator><author>Snape</author><pubDate>Sat, 31 Mar 2012 21:51:00 GMT</pubDate><guid>http://www.shnenglu.com/chenglong7997/articles/169730.html</guid><wfw:comment>http://www.shnenglu.com/chenglong7997/comments/169730.html</wfw:comment><comments>http://www.shnenglu.com/chenglong7997/articles/169730.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/chenglong7997/comments/commentRss/169730.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/chenglong7997/services/trackbacks/169730.html</trackback:ping><description><![CDATA[<h2><a id="cb_post_title_url" style="color: #0066ff; text-decoration: none; ">如何学好C++语言Q{Q?/a></h2><div style="padding-left: 5px; line-height: 1.5; color: #333333; font-family: Verdana, Arial, sans-serif, 'Lucida Grande'; text-align: left; background-color: #d6d3d6; "><div class="ge8sk84" id="cnblogs_post_body" style="line-height: 1.8; "><p>原文地址Q?a style="color: #0066ff; text-decoration: none; ">http://coolshell.cn/articles/4119.html</a></p><p>因ؓ之前收藏的地址找不CQ所以重新发一?/p><p>昨天写了一?a title="如何学好C语言" target="_blank" style="color: #0066ff; text-decoration: none; ">如何学好C语言</a>Q就有h回复问我如何学好C++Q所以,我把我个人的一些学习经验写在这里,希望对大家有用。首先,因ؓ<a title="如何学好C语言" target="_blank" style="color: #0066ff; text-decoration: none; ">如何学好C语言</a>中谈C法和系l,所以这里就只谈C++语言?/p><ul style="margin-left: 45px; "><li style="font-size: 10pt; list-style: inherit; "><strong>C++是最隄语言</strong>。这个世界上最隄~程语言可能非C++莫属了。你千万不要以ؓ<a title="“21天教你学会C++”" target="_blank" style="color: #0066ff; text-decoration: none; ">几天可以学好C++</a>QC++的学习曲U是相当BT的,你可以看?a title="C++ E序员自信心曲线? target="_blank" style="color: #0066ff; text-decoration: none; ">q篇文章</a>。C++是一门很自由的语aQ自由到了有?a title="恐怖的C++语言" target="_blank" style="color: #0066ff; text-decoration: none; ">BT和恐怖的地步</a>。我甚至认ؓC++q不是一门成熟的~程语言Q因为太Ҏ犯错了。所以,<strong>你一定要在一开始就要有很小心}慎的态度Qƈ把C++当成一U难以训服的猛兽来看?/strong>?/li></ul><ul style="margin-left: 45px; "><li style="font-size: 10pt; list-style: inherit; "><strong>多问“Z么要q样”的问?/strong>。学习C++一定要多问几个“Z么是q样”Q?#8220;凭什么要q样”的问题。比如:很多人知道C++有拷贝构造函数和初始化列表,但你真的知道Z么要有拷贝构造函敎ͼZ么要有初始化列表吗?Z么要有templateQؓ什么要有RTTIQؓ什么不是别的呢Q难道就是ؓ了让一门语a变得Cool一些吗Q完全不是这LQC++中的M一个feature都有些实实在在的原因Q?strong>你一定要M解ؓ什么要把C++设计成这L原因Q你才能学好C++</strong>。有I看看?a target="_blank" style="color: #0066ff; text-decoration: none; ">C++演化和设?/a>》一书?/li></ul><p> </p><ul style="margin-left: 45px; "><li style="font-size: 10pt; list-style: inherit; "><strong>看书Q大量的C++?/strong>。你可以按如下先后顺序阅读(下面q些书,我花了大U?-5q的旉Q今天我q在随时温习Q?ul style="list-style-type: disc; margin-left: 45px; "><li style="font-size: 10pt; list-style: inherit; ">?a target="_blank" style="color: #0066ff; text-decoration: none; ">C++ Primer</a>》,q本初L可能让会你啃得很痛苦Q所有的语言的特性和Z么都在里面了Q好好读诅R当然由C++之父写的?a target="_blank" style="color: #0066ff; text-decoration: none; ">C++E序设计语言</a>》也不错。两本看一本就好了Q我看的是前者)?/li><li style="font-size: 10pt; list-style: inherit; ">了解C++的语法仅仅是万里长征的第一步,你还需要看看?a target="_blank" style="color: #0066ff; text-decoration: none; ">Effective C++</a>》和?a target="_blank" style="color: #0066ff; text-decoration: none; ">More Effective C++</a>》这两本书ƈ不厚Q但我从02q就一直看到现在,每次L都有新的体会Q这两本书太l典了。如果你对C语言不熟Q这两本书会让你回去补C语言的课?/li><li style="font-size: 10pt; list-style: inherit; "><a target="_blank" style="color: #0066ff; text-decoration: none; ">Think in C++</a>同样是另一本经怹极的书,学c++必读Q但是中文版的翻译的很不好,所以还是去读英文版的吧?/li><li style="font-size: 10pt; list-style: inherit; ">?a target="_blank" style="color: #0066ff; text-decoration: none; ">C++沉思录</a>》同样非常值得一读,q里教的不是~程Q而是思考的ҎQ这是相当珍늚?/li><li style="font-size: 10pt; list-style: inherit; ">?a target="_blank" style="color: #0066ff; text-decoration: none; ">Exceptional C++</a>》和?a target="_blank" style="color: #0066ff; text-decoration: none; ">More Exceptional C++</a>》让你看看各U问题的解决Ҏ和一些常见的l典错误?/li><li style="font-size: 10pt; list-style: inherit; ">?a target="_blank" style="color: #0066ff; text-decoration: none; ">Advanced C++</a>》和?a target="_blank" style="color: #0066ff; text-decoration: none; ">Modern C++</a>》可以让你知道C++各种奇的用法?/li><li style="font-size: 10pt; list-style: inherit; ">?a target="_blank" style="color: #0066ff; text-decoration: none; ">泛型~程与STL</a>》是把C++实践C极致的东ѝ很强大。STL——一L模板库(容器Q算法和函数对象Q,不得不服?/li><li style="font-size: 10pt; list-style: inherit; ">?a target="_blank" style="color: #0066ff; text-decoration: none; ">深入探烦C++对象模型</a>》让你了解编译器下的C++是什么样的,让你了解C++的性能q不差。这个对于C++的程序员太关键了。我以前写过的?a target="_blank" style="color: #0066ff; text-decoration: none; ">C++虚函数表解析</a>》还有?a target="_blank" style="color: #0066ff; text-decoration: none; ">C++对象内存布局</a>》属于这个范畴?/li></ul></li></ul><ul style="margin-left: 45px; "><li style="font-size: 10pt; list-style: inherit; "><strong>和Java语言做对?/strong>。我个h以ؓJava对C++q个q不成熟的语a做了很多调整Q规范和限制。所以,Ҏ一下Java和C++Q想一惻IZ么一些东西在C++中可以做Q但在Java中却不行。比如:Java的异常是必需要catch的,不然׃~译不通过。ؓ什么Java不提供操作符重蝲Qؓ什么Java会引入接口来做多重承?Z么Java没有像C++那样的I/O字符?Z么Java不支持指针?Z么Java可以做到垃圾回收Q等{?strong>Java体现着很多面向对象设计的东西,学习Java有助于你学会怎么更好C用C++来编E?/strong>?/li></ul><ul style="margin-left: 45px; "><li style="font-size: 10pt; list-style: inherit; "><strong>面向对象设计</strong> 。虽?a title="面向对象是个骗局Q!" target="_blank" style="color: #0066ff; text-decoration: none; ">面向对象可能是个骗局</a>。但是我觉得面向对象设计中的一些实践非常的不错Q比如,单一原则Q依赖倒置原则Q等{,都非常地l典。?a target="_blank" style="color: #0066ff; text-decoration: none; ">设计模式</a>》必需一读,?a target="_blank" style="color: #0066ff; text-decoration: none; ">面向对象的分析和设计</a>》可以一诅R?strong>但不可以设计模式Z心来~程Q而应该是用设计模式来解藕</strong>?/li></ul><ul style="margin-left: 45px; "><li style="font-size: 10pt; list-style: inherit; "><strong>cd学习</strong>。看看MFC是怎么装Windows API的,看看ACE是怎么面向对象的,看看boost是怎么玩面向对象的Q看看CPPUnit又是怎么设计的。当Ӟ<a title="JDK里的设计模式" target="_blank" style="color: #0066ff; text-decoration: none; ">Java的JDK中有太多的设计模?/a>Q可以参考?/li></ul><p>希望没有吓到大家QƈƢ迎大家补充?/p><p><em><strong>—————更新 2011/03/30 19:20————</strong></em></p><p>更新几个观点Q?/p><ul style="margin-left: 45px; "><li style="font-size: 10pt; list-style: inherit; ">1Q我不擅长写书评Q所以推荐的q些书可能会让你有点看点没有感觉Q你可以上豆瓣或是China-pub上看看书评?/li><li style="font-size: 10pt; list-style: inherit; ">2QC++有很多奇淫技巧,有的很BTQ包括虚函数表,也许会有得有Ҏ意思,但我觉得很有意思,一斚w可以了解一门语a的实现细节,另一斚w可以开阔思\。我从学习这些知识中受益很多?/li><li style="font-size: 10pt; list-style: inherit; ">3Q上q是我的个h的学习历E,我觉得对我很有效Q所以是l验之谈?/li><li style="font-size: 10pt; list-style: inherit; ">4Q这cȝ文章在网上有很多很多Q我不是W一个写q样的文章,我也不是写得最好的Q我q不希望用长大论来谈论什么。只是想l大家了解一下大概的学习样子。毕竟,C++博大_深QQ何一文章都无法说好。不如就单一些?/li></ul></div></div><img src ="http://www.shnenglu.com/chenglong7997/aggbug/169730.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/chenglong7997/" target="_blank">Snape</a> 2012-04-01 05:51 <a href="http://www.shnenglu.com/chenglong7997/articles/169730.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一个类的实例化对象所占空间的大小http://www.shnenglu.com/chenglong7997/articles/169729.htmlSnapeSnapeSat, 31 Mar 2012 19:25:00 GMThttp://www.shnenglu.com/chenglong7997/articles/169729.htmlhttp://www.shnenglu.com/chenglong7997/comments/169729.htmlhttp://www.shnenglu.com/chenglong7997/articles/169729.html#Feedback0http://www.shnenglu.com/chenglong7997/comments/commentRss/169729.htmlhttp://www.shnenglu.com/chenglong7997/services/trackbacks/169729.html阅读全文

Snape 2012-04-01 03:25 发表评论
]]>
extern "C"http://www.shnenglu.com/chenglong7997/articles/169628.htmlSnapeSnapeSat, 31 Mar 2012 03:43:00 GMThttp://www.shnenglu.com/chenglong7997/articles/169628.htmlhttp://www.shnenglu.com/chenglong7997/comments/169628.htmlhttp://www.shnenglu.com/chenglong7997/articles/169628.html#Feedback0http://www.shnenglu.com/chenglong7997/comments/commentRss/169628.htmlhttp://www.shnenglu.com/chenglong7997/services/trackbacks/169628.html?/h3>  extern "C" 包含双重含义Q从字面上即可得刎ͼ首先Q被它修饰的目标?#8220;extern”的;其次Q被它修饰的目标?#8220;C”的。让我们来详l解读这两重含义?br />含义
  Q?Q?被extern 限定?/span>函数?/span>变量是externcd的:
  a. extern修饰变量的声明。D例来_如果文ga.c需要引用b.c中变量int vQ就可以在a.c中声明extern int vQ然后就可以引用变量v。这里需要注意的是,被引用的变量v的链接属性必L外链接(externalQ的Q也是说a.c要引用到vQ不只是取决于在a.c中声明extern int vQ还取决于变量v本n是能够被引用到的。这涉及到c语言的另外一个话题-Q变量的作用域。能够被其他模块以extern修饰W引用到的变量通常是全局变量。还有很重要的一ҎQextern int v可以攑֜a.c中的M地方Q比如你可以在a.c中的函数fun定义的开头处声明extern int vQ然后就可以引用到变量v了,只不q这样只能在函数fun作用域中引用v|了Q这q是变量作用域的问题。对于这一Ҏ_很多Z用的时候都心存虑。好像extern声明只能用于文g作用域似的?/span>
  b. extern修饰函数声明。从本质上来Ԍ变量和函数没有区别。函数名是指向函Cq制块开头处的指针。如果文件a.c需要引用b.c中的函数Q比如在b.c中原型是int fun(int mu)Q那么就可以在a.c中声明extern int funQint muQ,然后p使用fun来做M事情。就像变量的声明一Pextern int funQint muQ可以放在a.c中Q何地方,而不一定非要放在a.c的文件作用域的范围中。对其他模块中函数的引用Q最常用的方法是包含q些函数声明的头文g。用extern和包含头文g来引用函数有什么区别呢Qextern的引用方式比包含头文件要z得多!extern的用方法是直接了当的,惛_用哪个函数就用extern声明哪个函数。这大概是KISS原则的一U体现吧Q这样做的一个明昄好处是,会加速程序的~译Q确切的说是预处理)的过E,节省旉。在大型CE序~译q程中,q种差异是非常明昄?/span>
  Q?Q?被extern "C"修饰的变量和函数是按照C语言方式~译和连接的Q?/span>
  未加extern “C”声明时的~译方式?/span>
  首先看看C++中对cMC的函数是怎样~译的?/span>
  作ؓ一U?/span>面向对象的语aQC++支持函数重蝲Q而过E式语言C则不支持。函数被C++~译后在W号库中的名字与C语言的不同。例如,假设某个函数的原型ؓQ?/span>
  voidfoo( int x, int y );
  该函数被C~译器编译后在符号库中的名字为_fooQ而C++~译器则会生像_foo_int_int之类的名?/span>Q不同的~译器可能生成的名字不同Q但是都采用了相同的机制Q生成的新名字称?#8220;mangled name”Q。_foo_int_intq样的名字包含了函数名、函数参数数量及cd信息QC++是靠这U机制来实现函数重蝲的。例如,在C++中,函数void foo( int x, int y )与void foo( int x, float y )~译生成的符h不相同的Q后者ؓ_foo_int_float?/span>
  同样圎ͼC++中的变量除支?/span>局部变?/a>外,q支持类成员变量和全局变量。用h~写E序的类成员变量可能与全局变量同名Q我们以"."来区分。而本质上Q编译器在进行编译时Q与函数的处理相|也ؓcM的变量取了一个独一无二的名字,q个名字与用L序中同名的全局变量名字不同?/span>
  未加extern "C"声明时的q接方式
  假设在C++中,模块A的头文g如下Q?/span>
  // 模块A头文件 moduleA.h
  #ifndef MODULE_A_H
  #define MODULE_A_H
  int foo( int x, int y );
  #endif
  在模块B中引用该函数Q?/span>
  // 模块B实现文g moduleB.cpp
  #i nclude "moduleA.h"
  foo(2,3);
  实际上,在连接阶D,q接器会从模块A生成的目标文件moduleA.obj中寻?/span>_foo_int_intq样的符P
  加extern "C"声明后的~译和连接方?/span>
  加extern "C"声明后,模块A的头文g变ؓQ?/span>
  // 模块A头文件 moduleA.h
  #ifndef MODULE_A_H
  #define MODULE_A_H
  extern "C" int foo( int x, int y );
  #endif
  在模块B的实现文件中仍然调用fooQ?,3Q,其结果是Q?/span>
  Q?Q模块A~译生成foo的目标代码时Q没有对其名字进行特D处理,采用了C语言的方式;
  Q?Q连接器在ؓ模块B的目标代码寻找foo(2,3)调用ӞL的是未经修改的符号名_foo?/span>
  如果在模块A中函数声明了foo为extern "C"cdQ而模块B中包含的是extern int foo( int x, int y ) Q则模块B找不到模块A中的函数Q反之亦然?/span>
  所以,可以用一句话概括extern “C”q个声明的真实目的(M语言中的M语法Ҏ的诞生都不是随意而ؓ的,来源于真实世界的需求驱动。我们在思考问题时Q不能只停留在这个语a是怎么做的Q还要问一问它Z么要q么做,动机是什么,q样我们可以更深入地理解许多问题Q:
  实现C++与C及其它语a的؜合编E?/span>
  明白了C++中extern "C"的设立动机,我们下面来具体分析extern "C"通常的用技巧?/span>

extern "C"的惯用法

  Q?Q?/span>在C++中引用C语言中的函数和变?/span>Q在包含C语言头文Ӟ假设为cExample.hQ时Q需q行下列处理Q?/span>
  extern "C"
  {
  #i nclude "cExample.h"
  }
  而在C语言的头文g中,对其外部函数只能指定为externcdQC语言中不支持extern "C"声明Q在.c文g中包含了extern "C"时会出现~译语法错误?/span>
  W者编写的C++引用C函数例子工程中包含的三个文g?/span>
源代?/a>如下Q?/span>
  /* 
c语言头文ӞcExample.h */
  #ifndef C_EXAMPLE_H
  #define C_EXAMPLE_H
  extern int add(int x,int y);
  #endif
  /* c语言实现文gQcExample.c */
  #i nclude "cExample.h"
  int add( int x, int y )
  {
  return x + y;
  }
  // c++实现文gQ调用addQcppFile.cpp
  extern "C"
  {
  #i nclude "cExample.h"
  }
  int main(int argc, char* argv[])
  {
  add(2,3);
  return 0;
  }
  Q?/span>注意q里如果用GCC~译的时?/span>Q请先用gcc -c选项生成cExample.oQ再使用g++ -o cppFile cppFile.cpp cExample.o才能生成预期的c++调用c函数的结果,否则Q用g++ -o cppFile cppFile.cpp cExample.c~译器会报错Q而当cppFile.cpp 文g中不使用下列语句
  extern "C"
  {
  #i nclude "cExample.h"
  }
  而改?/span>
  #i nclude "cExample.h"
  extern "C" int add( int x, int y );
  ?/span>
  g++ -o cppFile cppFile.cpp cExample.c的编译过E会把add函数按c++的方式解释ؓ_foo_int_intq样的符受?/span>
  Q?/span>
  如果C++调用一个C语言~写?DLLӞ当包?DLL的头文g或声明接口函数时Q应加extern "C" { }?/span>
  Q?Q?/span>在C中引用C++语言中的函数和变量时QC++的头文g需dextern "C"Q但是在C语言中不能直接引用声明了extern "C"的该头文Ӟ应该仅将C文g中将C++中定义的extern "C"函数声明为externcd?/span>
  W者编写的C引用C++函数例子工程中包含的三个文g的源代码如下Q?/span>
  //C++头文?cppExample.h
  #ifndef CPP_EXAMPLE_H
  #define CPP_EXAMPLE_H
  extern "C" int add( int x, int y );
  #endif
  //C++实现文g cppExample.cpp
  #i nclude "cppExample.h"
  int add( int x, int y )
  {
  return x + y;
  }
  /* C实现文g cFile.c
  /* q样会编译出错:#i nclude "cppExample.h" */
  extern int add( int x, int y );
  int main( int argc, char* argv[] )
  {
  add( 2, 3 );
  return 0;
  } 

Snape 2012-03-31 11:43 发表评论
]]>
C/C++. filename, line, onexithttp://www.shnenglu.com/chenglong7997/articles/169589.htmlSnapeSnapeFri, 30 Mar 2012 18:11:00 GMThttp://www.shnenglu.com/chenglong7997/articles/169589.htmlhttp://www.shnenglu.com/chenglong7997/comments/169589.htmlhttp://www.shnenglu.com/chenglong7997/articles/169589.html#Feedback0http://www.shnenglu.com/chenglong7997/comments/commentRss/169589.htmlhttp://www.shnenglu.com/chenglong7997/services/trackbacks/169589.html

如何判断一D늨序是由C ~译E序q是由C++~译E序~译的? 
{案Q?nbsp;

  1. #ifdef __cplusplus  
  2. cout<<"c++";  
  3. #else  
  4. cout<<"c";  
  5. #endif  

 

如何打印出当前源文g的文件名以及源文件的当前行号Q?nbsp;
{案Q?nbsp;
cout << __FILE__ ; 
cout<<__LINE__ ; 
__FILE__和__LINE__是系l预定义宏,q种宏ƈ不是在某个文件中定义的,而是q译器定义的?/p>

main d数执行完毕后Q是否可能会再执行一D代码,l出说明Q?nbsp;
{案Q可以,可以用_onexit 注册一个函敎ͼ它会在main 之后执行?/p>

  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. int fn1()  
  5. {  
  6.     printf( "next.\n" );  
  7.     return 0;  
  8. }  
  9. int fn2()  
  10. {  
  11.     printf( "executed " );  
  12.     return 0;  
  13. }  
  14. int fn3()  
  15. {  
  16.     printf( "is " );  
  17.     return 0;  
  18. }  
  19. int fn4()  
  20. {  
  21.     printf( "This " );  
  22.     return 0;  
  23. }  
  24.   
  25. int _tmain(int argc, _TCHAR* argv[])  
  26. {     
  27.     _onexit( fn1 );  
  28.     _onexit( fn2 );  
  29.     _onexit( fn3 );  
  30.     _onexit( fn4 );  
  31.     printf( "This is executed first.\n" );  
  32.   
  33.     return 0;  
  34. }  
输出l果为:image

The _onexit function is passed the address of a function (func) to be called when the program terminates normally. Successive calls to _onexit create a register of functions that are executed in LIFO (last-in-first-out) order. The functions passed to _onexit cannot take parameters.

 

cL员函数的重蝲、覆盖和隐藏区别Q?nbsp;
{案Q?nbsp;
a.成员函数被重载的特征Q?nbsp;
Q?Q相同的范围Q在同一个类中)Q?nbsp;
Q?Q函数名字相同; 
Q?Q参C同; 
Q?Qvirtual 关键字可有可无?/p>

  Q?Qconst的区?nbsp;
b.覆盖是指zcd数覆盖基cd敎ͼ特征是: 
Q?Q不同的范围Q分别位于派生类与基c)Q?nbsp;
Q?Q函数名字相同; 
Q?Q参数相同; 
Q?Q基cd数必Lvirtual 关键字?nbsp;
c.“隐藏”是指zcȝ函数屏蔽了与其同名的基类函数Q规则如下: 
Q?Q如果派生类的函C基类的函数同名,但是参数不同。此Ӟ不论有无virtual关键字,基类的函数将被隐藏(注意别与重蝲hQ?nbsp;
Q?Q如果派生类的函C基类的函数同名,q且参数也相同,但是基类函数没有virtual 关键字。此Ӟ基类的函数被隐藏Q注意别与覆盖؜淆)



Snape 2012-03-31 02:11 发表评论
]]>L表存储图,深度和广度优先遍?/title><link>http://www.shnenglu.com/chenglong7997/articles/169225.html</link><dc:creator>Snape</dc:creator><author>Snape</author><pubDate>Tue, 27 Mar 2012 20:39:00 GMT</pubDate><guid>http://www.shnenglu.com/chenglong7997/articles/169225.html</guid><wfw:comment>http://www.shnenglu.com/chenglong7997/comments/169225.html</wfw:comment><comments>http://www.shnenglu.com/chenglong7997/articles/169225.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/chenglong7997/comments/commentRss/169225.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/chenglong7997/services/trackbacks/169225.html</trackback:ping><description><![CDATA[     摘要: 拿q个囑ց实验?#include<iostream>#include<vector>#include<queue> using namespace std; struct ArcNode{             //?..  <a href='http://www.shnenglu.com/chenglong7997/articles/169225.html'>阅读全文</a><img src ="http://www.shnenglu.com/chenglong7997/aggbug/169225.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/chenglong7997/" target="_blank">Snape</a> 2012-03-28 04:39 <a href="http://www.shnenglu.com/chenglong7997/articles/169225.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>最大子序列、最长公共子丌Ӏ最长公共子序列[转]http://www.shnenglu.com/chenglong7997/articles/169140.htmlSnapeSnapeTue, 27 Mar 2012 05:38:00 GMThttp://www.shnenglu.com/chenglong7997/articles/169140.htmlhttp://www.shnenglu.com/chenglong7997/comments/169140.htmlhttp://www.shnenglu.com/chenglong7997/articles/169140.html#Feedback0http://www.shnenglu.com/chenglong7997/comments/commentRss/169140.htmlhttp://www.shnenglu.com/chenglong7997/services/trackbacks/169140.html阅读全文

Snape 2012-03-27 13:38 发表评论
]]>
用C++设计一个不能被l承的类http://www.shnenglu.com/chenglong7997/articles/168959.htmlSnapeSnapeMon, 26 Mar 2012 00:33:00 GMThttp://www.shnenglu.com/chenglong7997/articles/168959.htmlhttp://www.shnenglu.com/chenglong7997/comments/168959.htmlhttp://www.shnenglu.com/chenglong7997/articles/168959.html#Feedback0http://www.shnenglu.com/chenglong7997/comments/commentRss/168959.htmlhttp://www.shnenglu.com/chenglong7997/services/trackbacks/168959.html题目Q用C++ 设计一个不能被l承的类?/span>
分析Q这是Adobe 公司2007 q校园招聘的最新笔试题。这道题除了考察应聘者的C++ 基本功底外,q能考察反应能力Q是一道很好的题目?/div>
在Java 中定义了关键字final Q被final 修饰的类不能被ѝ但在C++ 中没有final q个关键字,要实现这个要求还是需要花费一些精力?/div>
首先惛_的是在C++ 中,子类的构造函C自动调用父类的构造函数。同P子类的析构函C会自动调用父cȝ析构函数。要想一个类不能被承,我们只要把它的构造函数和析构函数都定义ؓU有函数。那么当一个类试图从它那承的时候,必然会由于试图调用构造函数、析构函数而导致编译错误?/div>
可是q个cȝ构造函数和析构函数都是U有函数了,我们怎样才能得到该类的实例呢Q这难不倒我们,我们可以通过定义静态来创徏和释攄的实例。基于这个思\Q我们可以写出如下的代码Q?/div>
///////////////////////////////////////////////////////////////////////
// Define a class which can't be derived from
///////////////////////////////////////////////////////////////////////
class FinalClass1
{
public :
      static FinalClass1GetInstance()
      {
            return new FinalClass1;
      }
 
      static void DeleteInstanceFinalClass1pInstance)
      {
            delete pInstance;
            pInstance = 0;
      }
 
private :
      FinalClass1() {}
      ~FinalClass1() {}
};
q个cL不能被承,但在总觉得它和一般的cL些不一P使用h也有点不方便。比如,我们只能得到位于堆上的实例,而得不到位于栈上实例?/div>
能不能实C个和一般类除了不能被承之外其他用法都一LcdQ办法L有的Q不q需要一些技巧。请看如下代码:
///////////////////////////////////////////////////////////////////////
// Define a class which can't be derived from
///////////////////////////////////////////////////////////////////////
template <typename Tclass MakeFinal
{
      friend T;
 
private :
      MakeFinal() {}
      ~MakeFinal() {}
};
 
class FinalClass2 : virtual public MakeFinal<FinalClass2>
{
public :
      FinalClass2() {}
      ~FinalClass2() {}
};
q个cM用v来和一般的cL有区别,可以在栈上、也可以在堆上创建实例。尽类 MakeFinal <FinalClass2> 的构造函数和析构函数都是U有的,但由于类 FinalClass2 是它的友元函敎ͼ因此?span style="font-size: 10pt; color: #010001; line-height: 16px; "> FinalClass2 中调?/span> MakeFinal <FinalClass2> 的构造函数和析构函数都不会造成~译错误?/div>
但当我们试图?span style="font-size: 10pt; color: #010001; line-height: 16px; "> FinalClass2 l承一个类q创建它的实例时Q却不同通过~译?/div>
class Try : public FinalClass2
{
public :
      Try() {}
      ~Try() {}
};
 
Try temp;
׃c?span style="font-size: 10pt; color: #010001; line-height: 16px; "> FinalClass2 是从c?span style="font-size: 10pt; color: #010001; line-height: 16px; "> MakeFinal <FinalClass2> 虚承过来的Q在调用 Try 的构造函数的时候,会直接蟩q?span style="font-size: 10pt; color: #010001; line-height: 16px; "> FinalClass2 而直接调?span style="font-size: 10pt; color: #010001; line-height: 16px; "> MakeFinal<FinalClass2> 的构造函数。非帔R憄是, Try 不是 MakeFinal <FinalClass2> 的友元,因此不能调用其私有的构造函数?/div>
Z上面的分析,试图?span style="font-size: 10pt; color: #010001; "> FinalClass2 l承的类Q一旦实例化Q都会导致编译错误,因此?span style="font-size: 10pt; color: #010001; "> FinalClass2 不能被ѝ这满了我们设计要求?/div>


Snape 2012-03-26 08:33 发表评论
]]>C语言 二维数组动态分配和释放 [转]http://www.shnenglu.com/chenglong7997/articles/168955.htmlSnapeSnapeSun, 25 Mar 2012 23:49:00 GMThttp://www.shnenglu.com/chenglong7997/articles/168955.htmlhttp://www.shnenglu.com/chenglong7997/comments/168955.htmlhttp://www.shnenglu.com/chenglong7997/articles/168955.html#Feedback0http://www.shnenglu.com/chenglong7997/comments/commentRss/168955.htmlhttp://www.shnenglu.com/chenglong7997/services/trackbacks/168955.html
C 二维数组动态分配和释放
(1)已知W二l?/div>
Code-1
char (*a)[N];//指向数组的指?/span>
a = (char (*)[N])malloc(sizeof(char *) * m);
printf("%d\n", sizeof(a));//4Q指?/span>
printf("%d\n", sizeof(a[0]));//NQ一l数l?/span>
free(a);
(2)已知W一l?/div>
Code-2
char* a[M];//指针的数l?/span>
int i;
for(i=0; i<M; i++)
a[i] = (char *)malloc(sizeof(char) * n);
printf("%d\n", sizeof(a));//4*MQ指针数l?/span>
printf("%d\n", sizeof(a[0]));//4Q指?/span>
for(i=0; i<M; i++)
   free(a[i]);
(3)已知W一l_一ơ分配内?保证内存的连l?
Code-3
char* a[M];//指针的数l?/span>
int i;
a[0] = (char *)malloc(sizeof(char) * M * n);
for(i=1; i<M; i++)
a[i] = a[i-1] + n;
printf("%d\n", sizeof(a));//4*MQ指针数l?/span>
printf("%d\n", sizeof(a[0]));//4Q指?/span>
free(a[0]);
(4)两维都未?/div>
Code-4
char **a;
int i;
a = (char **)malloc(sizeof(char *) * m);//分配指针数组
for(i=0; i<m; i++)
{
a[i] = (char *)malloc(sizeof(char) * n);//分配每个指针所指向的数l?/span>
}
printf("%d\n", sizeof(a));//4Q指?/span>
printf("%d\n", sizeof(a[0]));//4Q指?/span>
for(i=0; i<m; i++)
{
free(a[i]);
}
free(a);
(5)两维都未知,一ơ分配内?保证内存的连l?
Code-5
char **a;
int i;
a = (char **)malloc(sizeof(char *) * m);//分配指针数组
a[0] = (char *)malloc(sizeof(char) * m * n);//一ơ性分配所有空?/span>
for(i=1; i<m; i++)
{
a[i] = a[i-1] + n;
}
printf("%d\n", sizeof(a));//4Q指?/span>
printf("%d\n", sizeof(a[0]));//4Q指?/span>
free(a[0]);
free(a);
2.C++动态分配二l数l?/strong>
(1)已知W二l?/div>
Code-6
char (*a)[N];//指向数组的指?/span>
a = new char[m][N];
printf("%d\n", sizeof(a));//4Q指?/span>
printf("%d\n", sizeof(a[0]));//NQ一l数l?/span>
delete[] a;
(2)已知W一l?/div>
Code-7
char* a[M];//指针的数l?/span>
for(int i=0; i<M; i++)
   a[i] = new char[n];
printf("%d\n", sizeof(a));//4*MQ指针数l?/span>
printf("%d\n", sizeof(a[0]));//4Q指?/span>
for(i=0; i<M; i++)
   delete[] a[i];
(3)已知W一l_一ơ分配内?保证内存的连l?
Code-8
char* a[M];//指针的数l?/span>
a[0] = new char[M*n];
for(int i=1; i<M; i++)
a[i] = a[i-1] + n;
printf("%d\n", sizeof(a));//4*MQ指针数l?/span>
printf("%d\n", sizeof(a[0]));//4Q指?/span>
delete[] a[0];
(4)两维都未?/div>
Code-9
char **a;
a = new char* [m];//分配指针数组
for(int i=0; i<m; i++)
{
a[i] = new char[n];//分配每个指针所指向的数l?/span>
}
printf("%d\n", sizeof(a));//4Q指?/span>
printf("%d\n", sizeof(a[0]));//4Q指?/span>
for(i=0; i<m; i++)
delete[] a[i];
delete[] a;
(5)两维都未知,一ơ分配内?保证内存的连l?
Code-10
char **a;
a = new char* [m];
a[0] = new char[m * n];//一ơ性分配所有空?/span>
for(int i=1; i<m; i++)
{
a[i] = a[i-1] + n;//分配每个指针所指向的数l?/span>
}
printf("%d\n", sizeof(a));//4Q指?/span>
printf("%d\n", sizeof(a[0]));//4Q指?/span>
delete[] a[0];
delete[] a;
多说一句:new和delete要注意配对用,x多少个new有多少个deleteQ这h可以避免内存泄漏Q?/span>
3.静态二l数l作为函数参C?/strong>
如果采用上述几种Ҏ动态分配二l数l,那么对应的数据cd作ؓ函数参数可以了。这里讨论静态二l数l作为函数参C递,x照以下的调用方式Q?/div>
int a[2][3];
func(a);
C语言中将静态二l数l作为参C递比较麻烦,一般需要指明第二维的长度,如果不给定第二维长度Q则只能先将其作Zl指针传递,然后利用二维数组的线性存储特性,在函C内{化ؓҎ定元素的讉K?/div>
首先写好试代码Q以验证参数传递的正确性:
(1)l定W二l长?/div>
Code-11
void func(int a[][N])
{
printf("%d\n", a[1][2]);
}
(2)不给定第二维长度
Code-12
void func(int* a)
{
printf("%d\n", a[1 * N + 2]);//计算元素位置
}

注意Q用该函数旉要将二维数组首地址强制转换Zl指针,即func((int*)a);



Snape 2012-03-26 07:49 发表评论
]]> 26uuuþ| Ʒݾþþþø | 69Ʒþþþþ޾Ʒ| þù߳׽ѹۿ| þþþþþþƷɫ | ޡvþþ뾫Ʒ| þٸ۲AV| þó18վ| 99þù޸ۿ2024 | 鶹AVһþ| 2021ƷþþƷ| ѾþþƷ99reѾy| yy6080þ| ޹Ʒһþhs | þۺϾɫۺ97_þþ| 97þù޾Ʒ| ƷžžþùھƷ| þþƷۺɫ| һþaþþƷۺ| 91Ʒ91þþþþ| þ99ƷþþþþҰ| ŷþһwwwwww.| ȾþӾþþƷ18| þþþþŮ| ޹һ˾þþƷ| ձþþþĻ | 99þþƷһ| þþƷĻ| ޹˾Ʒ91þþ | ɫ͵͵91þۺ| ھƷ˾þþӰԺ| ɫþAVۺ| ˾þƵ| ˾þóۺӰԺ| ˾þô߽AV | ޾ƷþëƬ| AVպƷþþþþþ| þþƷh| ޾ƷWWWþþþþ| ھƷþþþӰԺձ| þùۺϾƷ|