﻿<?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++博客-step by step</title><link>http://www.cppblog.com/xiaoluoluo/</link><description /><language>zh-cn</language><lastBuildDate>Wed, 22 Apr 2026 02:27:46 GMT</lastBuildDate><pubDate>Wed, 22 Apr 2026 02:27:46 GMT</pubDate><ttl>60</ttl><item><title>散列3</title><link>http://www.cppblog.com/xiaoluoluo/archive/2009/11/27/102070.html</link><dc:creator>小罗罗</dc:creator><author>小罗罗</author><pubDate>Fri, 27 Nov 2009 09:14:00 GMT</pubDate><guid>http://www.cppblog.com/xiaoluoluo/archive/2009/11/27/102070.html</guid><wfw:comment>http://www.cppblog.com/xiaoluoluo/comments/102070.html</wfw:comment><comments>http://www.cppblog.com/xiaoluoluo/archive/2009/11/27/102070.html#Feedback</comments><slash:comments>7</slash:comments><wfw:commentRss>http://www.cppblog.com/xiaoluoluo/comments/commentRss/102070.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xiaoluoluo/services/trackbacks/102070.html</trackback:ping><description><![CDATA[<span style="FONT-SIZE: 12pt">&nbsp; 散列这是最后一章了，快毕业了，这几天赶快把论文写完，到回家还不到两个月，答应张老师再去实验室把做的东西总结一下，其实打算在实验室再呆两个月，腊月底再回，想写的东西嘛，我想研究一下opencv的内存管理机制，结合我买的那本Applied C++,顺便推销一下这本书，这本书很薄，两百多页，介绍如何应用c++来解决开发商业软件时所固有的问题，最难能可贵的是，从头至尾提供了一个图像处理框架，对于想在数字图像处理，机器视觉方向深入探究（不是具体算法，而是整个软件架构）有挺大的启发意义的(虽然网上评价不是太好，可能比较牛的人看不上吧，也有可能这本书比较偏重于数字图像领域)，还想学的东西呢，年前和年后几个月的时间Bjarne Stroustrup的那本c++,Mark Allen Weiss的那本数据结构，Jon Kleinberg 的那本算法设计，后两本书是俺在图像室的图书角找到的，非常的不错哦，可惜毕业前就要还，正好督促我赶紧看，在联合书城看到Richard Johnsonbaugh的Discrete Mathematics,竟然是第七版了，只能怪我资质太差看不懂knuth的那三本圣经，只好咬着牙买下来先琢磨琢磨了算是打基础了，公司有项目做嵌入式平台上的编译器，要是有时间的话看看嵌入式操作系统和编译原理吧，很想写个编译器，这么多好书要看，有时候真不想回家过年了，嘿嘿，说着玩的，到时候家里肯定杀猪了，不回去真是太可惜了。<br><br>1.再散列<br>&nbsp;&nbsp; 其实就是前两篇中有提到的rehash了，对于使用平方探测的开放定制散列法，如果表的元素填得太满，那么操作的运行时间将开始消耗过长，且插入操作可能失败。这可能发生在有太多删除和插入混合的场合。此时，一个解决方法是建立另外一个大约两倍大的表(而且使用一个相关的新散列函数)，扫描整个原始散列表，计算每个(未删除的)元素的新散列值并将其插入到新表中。整个操作成为再散列(rehashing)。这显然是一种非常昂贵的操作；其运行时间为O(N),因为有N个元素要再散列而且表的大小约为2N，不过，因为不是经常发生，所以实际效果并没有这么差。特别是，在最后的再散列之前已经存在N/2次插入，因此添加到每个插入上的花费基本是一个常数开销。如果这种数据结构是程序的一部分，那么其影响是不明显的。另一方面，如果再散列作为交互系统一部分运行，那么其插入引起再散列的用户将会感到速度减慢。<br>&nbsp;&nbsp;&nbsp; 再散列可以用平方预测以多种方法实现。一种做法是只要表满一半就再散列。另一种极端的方法是只有当插入失败时才再散列。第三种方法即途中(middle-of-the-road)策略：当表到达某一个装填因子时进行再散列。由于随着装填因子的增加，表的性能的确在下降，因此，以好的截至点实现的第三种策略，可能是最好的策略。<br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: small; 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: #008000">//</span><span style="COLOR: #008000">对探测散列表和分离链接散列表的再散列</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;rehash()<br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img id=Codehighlighter1_35_329_Open_Image onclick="this.style.display='none'; Codehighlighter1_35_329_Open_Text.style.display='none'; Codehighlighter1_35_329_Closed_Image.style.display='inline'; Codehighlighter1_35_329_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_35_329_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_35_329_Closed_Text.style.display='none'; Codehighlighter1_35_329_Open_Image.style.display='inline'; Codehighlighter1_35_329_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_35_329_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_35_329_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">HashEntry</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;oldArray&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;array;<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;array.resize(&nbsp;nextPrime(&nbsp;</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;oldArray.size()&nbsp;)&nbsp;);<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>&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;j&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">array.size();&nbsp;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">&nbsp;)<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;&nbsp;&nbsp;&nbsp;array[j].info&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;EMPTY;<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>&nbsp;&nbsp;&nbsp;&nbsp;currentSize&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">&nbsp;9</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&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</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">oldArray.size();&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">&nbsp;)<br></span><span style="COLOR: #008080">10</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">(&nbsp;oldArray[i].info&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;ACTIVE&nbsp;)<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insert(&nbsp;oldArray[i].element&nbsp;);<br></span><span style="COLOR: #008080">12</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">13</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;rehash()<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img id=Codehighlighter1_346_704_Open_Image onclick="this.style.display='none'; Codehighlighter1_346_704_Open_Text.style.display='none'; Codehighlighter1_346_704_Closed_Image.style.display='inline'; Codehighlighter1_346_704_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_346_704_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_346_704_Closed_Text.style.display='none'; Codehighlighter1_346_704_Open_Image.style.display='inline'; Codehighlighter1_346_704_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_346_704_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_346_704_Open_Text><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>&nbsp;&nbsp;&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">list</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">HashedObj</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;oldLists&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;theLists;<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;theLists.resize(&nbsp;nextPrime(&nbsp;</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;theLists.size()&nbsp;)&nbsp;);<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;&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;j&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">theLists.size();&nbsp;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">&nbsp;)<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;theLists[j].clear();<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;currentSize&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">21</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&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</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">oldLists.size();&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">&nbsp;)<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img id=Codehighlighter1_580_702_Open_Image onclick="this.style.display='none'; Codehighlighter1_580_702_Open_Text.style.display='none'; Codehighlighter1_580_702_Closed_Image.style.display='inline'; Codehighlighter1_580_702_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_580_702_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_580_702_Closed_Text.style.display='none'; Codehighlighter1_580_702_Open_Image.style.display='inline'; Codehighlighter1_580_702_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_580_702_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_580_702_Open_Text><span style="COLOR: #000000">{<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;list</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">HashedObj</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::iterator&nbsp;itr&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;OldLists[i].begin();<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;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;(&nbsp;itr&nbsp;</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">&nbsp;oldLists[i].end()&nbsp;)<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;insert(&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">itr</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">&nbsp;);<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;}</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/ExpandedBlockEnd.gif" align=top>}</span></span></div>
<p></span><br><span style="FONT-SIZE: 12pt">2.标准库中的散列表<br>&nbsp;&nbsp;&nbsp; 标准库中不包括set和map的散列表实现。但是，许多的编译器提供具有与set和map类相同的成员函数的hash_set和hash_map.<br>&nbsp;&nbsp;&nbsp; 要使用hash_set和hash_map，就必须有相应的包含指令，而且，可能也需要相应的命名空间。这两者都是和编译器相关的。接下来还必须提供相应的类型参数来说明<br>hash_set和hash_map。对于hash_map，这些类型参数包括键的类型，值的类型，散列函数(返回无符号整数)和一个相等性操作符。遗憾的是，至于键和值的类型参数如何<br>表示还是编译器相关的。<br>&nbsp;&nbsp;&nbsp; 下一次c++的较大的修订将不可避免地包括这些hash_set和hash_map中的一个。<br><br>3.可扩散列<br>&nbsp;&nbsp;&nbsp; 最后讨论数据量太大以至于装不进主存的情况，此时主要考虑的是检索数据所需的磁盘存取次数。假设在任意时刻都有N个记录要存储，N的值随时间而变化。此外，最多可把M个记录放入一个磁盘区块，设M=4,如果使用探测散列或分离链接散列，那么主要的问题在于，即使是理想分布的散列表，在一次查找操作中，冲突也可能引起对多个区块的访问。不仅如此，当表变得过慢的时候，必须执行代价巨大的再散列这一步，它需要O(N)的磁盘访问。<br>&nbsp;&nbsp;&nbsp; 一种聪明的选择成为可扩散列(extendible hashing),它允许用两次磁盘访问执行一次查找。插入操作也需要很少的磁盘访问.<br>&nbsp;&nbsp; <strong>Extendible hashing from Wikipedia<br>&nbsp;&nbsp; Extendible hashing</strong> is a type of hash system which treats a hash as a bit string, and uses a <a title=Trie href="http://en.wikipedia.org/wiki/Trie">trie</a> for bucket lookup. Because of the hierarchal nature of the system, re-hashing is an incremental operation (done one bucket at a time, as needed). This means that time-sensitive applications are less affected by table growth than by standard full-table rehashes.<br><br>&nbsp;&nbsp;&nbsp; </p>
<p>This is a more simplistic example from <a href="http://en.wikipedia.org/wiki/Extendible_hashing#CITEREFFaginNievergeltPippengerStrong1979">Fagin et al. (1979)</a>.</p>
<p>Assume that the hash function <span class=texhtml><em>h</em>(<em>k</em>)</span> returns a binary number. The first i bits of each string will be used as indices to figure out where they will go in the "directory" (hash table). Additionally, i is the smallest number such that the first i bits of all keys are different.</p>
<p>Keys to be used:</p>
<p><span class=texhtml><em>h</em>(<em>k</em><sub>1</sub>)</span> = 100100<br><span class=texhtml><em>h</em>(<em>k</em><sub>2</sub>)</span> = 010110<br><span class=texhtml><em>h</em>(<em>k</em><sub>3</sub>)</span> = 110110<br></p>
<p>Let's assume that for this particular example, the bucket size is 1. The first two keys to be inserted, k<sub>1</sub> and k<sub>2</sub>, can be distinguished by the most significant bit, and would be inserted into the table as follows:</p>
<pre> directory
---------
|    0    |-----------&gt; Bucket A (contains k2)
|---------|
|    1    |-----------&gt; Bucket B (contains k1)
---------
</pre>
<p>Now, if k<sub>3</sub> were to be hashed to the table, it wouldn't be enough to distinguish all three keys by one bit (because k<sub>3</sub> and k<sub>1</sub> have 1 as their leftmost bit. Also, because the bucket size is one, the table would overflow. Because comparing the first two most significant bits would give each key a unique location, the directory size is doubled as follows:</p>
<pre>  directory
----------
|    00    |-----\
|----------|      ----------&gt; Bucket A (contains k2)
|    01    |-----/
|----------|
|    10    |-----------&gt; Bucket B (contains k1)
|----------|
|    11    |-----------&gt; Bucket C (contains k3)
----------
</pre>
<p>And so now k<sub>1</sub> and k<sub>3</sub> have a unique location, being distinguished by the first two leftmost bits. Because k<sub>2</sub> is in the top half of the table, both 00 and 01 point to it because there is no other key to compare to that begins with a 0.</p>
4.小结<br>&nbsp;&nbsp;&nbsp; 散列表可以用来以常数平均时间实现insert和contains操作。当使用散列表时，注意诸如装填因子这样的细节是特别重要的，否则时间界将不再有效。当键不是短字符串或整数时，仔细选择散列函数也是很重要的。<br>&nbsp;&nbsp;&nbsp; 对于分离链接散列法，虽然装弹因子不大时性能并不明显降低，但装填因子还是应该接近于1，对于探测散列，除非完全不可避免，否则装填因子不应该超过0.5，如果使用线性探测，那么性能随着装填因子接近于1而急速下降。再散列运算可以通过使表增长(或收缩)来实现，这样可以保持合理的装填因子。对于空间紧缺并且不可能声明巨大散列表的情况，这是很重要的。<br>&nbsp;&nbsp;&nbsp; 二叉查找树也可以用来实现insert和contains操作。虽然平均时间界为O(logN)，但是二叉查找树也支持那些需要排序的例程，从而功能更强大，使用散列表不可能找出最小元素。除非准确知道一个字符串，否则散列表也不可能有效地查找它。二叉查找树可以迅速找到一定范围内的所有项，散列表却做不到。不仅如此，因为查找树不需要乘法和除法，O(logN)这个时间界也不必比O(1)大那么多。<br>&nbsp;&nbsp;&nbsp; 另一方面，散列的最坏情形一般来自于实现错误，而有序的输入却可能使二叉树运行得很差。平衡查找树实现的代价相当高。因此，如果不需要排序的信息或者不确定输入是否已经排序，那么就应该选择散列这种数据结构。<br>&nbsp;&nbsp;&nbsp; <span style="COLOR: #0000ff">散列的应用很广。编译器使用散列表跟踪源代码中声明的变量，这种数据结构叫做符号表(symbol table)。散列表时这种问题的理想选择。标识符一般都不长，因此散列函数能够迅速完成运算。此外，按字母顺序排序变量通常也是不必要的。<br>&nbsp;&nbsp;&nbsp; 散列表适用于任何其节点有实名而不是数字名的图论问题。这里，当输入被读入的时候，定点则按照它们出现的顺序从1开始指定为一些整数。再有，输入很可能有一组按字母顺序排列的项。例如，顶点可以是计算机。此时，如果一个特定的计算中心把它的计算机列表成ibm1,ibm2,ibm3...那么，若使用查找树则在效率方面很可能会有戏剧性的结果。<br>&nbsp;&nbsp; 散列表的第三种常见的用途实在为游戏编制的程序中。当程序搜索游戏的不同的运动路径时，它通过计算基于位置的散列函数而跟踪一些已知的位置(并把对于该位置的移动存储起来)。如果同样的位置再次出现，程序通常通过简单的移动变换来避免昂贵的重复计算。游戏程序的这种一般特点叫做置换表(transposition table）.<br>&nbsp;&nbsp;&nbsp;散列的另一个用途是在线拼写检查程序。如果拼写检查程序的主要功能是检查拼写错误(而非纠正错误),那么可以预先将整个词典进行散列，这样就可以在常数时间内检查单词拼写。散列表很适合这项工作，因为以字母顺序排列单词并不重要，而以它们在文件中出现的顺序显示错误拼写当然也是可以接受的。</span></span> 
<img src ="http://www.cppblog.com/xiaoluoluo/aggbug/102070.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xiaoluoluo/" target="_blank">小罗罗</a> 2009-11-27 17:14 <a href="http://www.cppblog.com/xiaoluoluo/archive/2009/11/27/102070.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>老外谈future of c++</title><link>http://www.cppblog.com/xiaoluoluo/archive/2009/11/27/102021.html</link><dc:creator>小罗罗</dc:creator><author>小罗罗</author><pubDate>Thu, 26 Nov 2009 16:23:00 GMT</pubDate><guid>http://www.cppblog.com/xiaoluoluo/archive/2009/11/27/102021.html</guid><wfw:comment>http://www.cppblog.com/xiaoluoluo/comments/102021.html</wfw:comment><comments>http://www.cppblog.com/xiaoluoluo/archive/2009/11/27/102021.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/xiaoluoluo/comments/commentRss/102021.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xiaoluoluo/services/trackbacks/102021.html</trackback:ping><description><![CDATA[<p>&nbsp;&nbsp;&nbsp; 又去Bartosz的c++ in action论坛上转了转，看到一个老外问Bartosz哥哥future of c++的帖子,论坛比较冷清，没有c++er和javaer们的互相炮轰，Bartosz有意思的是没忘记向各位推销他的D语言。。。<br>&nbsp;</p>
<p><span style="COLOR: #ff0000"><strong>helmi:</strong></span></p>
<div class=postcolor id=post-3289>Hi, I just found this very useful site and I have read almost all of tutorial, guide, articles. <img style="VERTICAL-ALIGN: middle" alt=smile.gif src="http://www.relisoft.com/forums/style_emoticons/default/smile.gif" border=0 emoid=":)"><br><br>I just ask everybody opinion on the future of C/C++ because a lot of other programming language out there now days. What do you think since more applications develop as on web base platform? This is just my observation in my country. I really like coding in C++ because I can feel I'm doing low level programming. I do have write game using directX as a hobby but to sustain, I have need to write applications using php, asp .net and jsp.<br><br><br>ps: Sorry for my bad English. <!--ibf.attachment_3289--></div>
<br><strong style="COLOR: #ff0000">Bartosz：</strong>
<div class=postcolor id=post-3292>I see programming tasks as a pyramid. The base of the pyramid is formed by small tasks that don't require sophisticated languages or tools. That's where most of the programming is done. The top of the pyramid are tough tasks that just can't be done using Java or C#. Right now the language at the top is C++. There aren't very many complex tasks, so the top of the pyramid is rather narrow, but then the number of seasoned C++ programmers is also relatively small. In the United States, C++ programmers are in high demand (and are very well paid).<br><br>Will C++ keep its top position? I don't know. There are very many aspects of C++ that are very unsatisfactory. I am now involved in the development of the D language (http://digitalmars.com/d/), which has a chance of replacing C++. <!--ibf.attachment_3292--></div>
<!-- THE POST --><br><strong style="COLOR: #ff0000">helmi:<br></strong>
<div class=postcolor id=post-3295>"the number of seasoned C++ programmers is also relatively small. In the United States, C++ programmers are in high demand (and are very well paid)."<br><br>Really? I should go working in the United States <img style="VERTICAL-ALIGN: middle" alt=smile.gif src="http://www.relisoft.com/forums/style_emoticons/default/smile.gif" border=0 emoid=":)"> <br><br><br>I have read of about the D language in a game forum. It's has a lot of features and so powerful but still need improvement, good IDE etc. <!--ibf.attachment_3295--></div>
<!-- THE POST --><br><strong style="COLOR: #ff0000">Bartosz：</strong><br>
<div class=quotetop>QUOTE (helmi @ Feb 1 2007, 05:30 PM) <a href="http://www.relisoft.com/forums/index.php?act=findpost&amp;pid=3295"><img alt=* src="http://www.relisoft.com/forums/style_images/1/post_snapback.gif" border=0></a></div>
<div class=quotemain><!--quotec-->I have read of about the D language in a game forum. It's has a lot of features and so powerful but still need improvement, good IDE etc.<!--quoteend--></div>
<!--quoteeend--><br>That's right. It needs an IDE, tools, and libraries. But it has a good chance of acqiring them quickly. <br><br><strong style="COLOR: #ff0000">peter:</strong><br>
<div class=quotetop>QUOTE (Bartosz @ Feb 2 2007, 04:32 AM) <a href="http://www.relisoft.com/forums/index.php?act=findpost&amp;pid=3296"><img alt=* src="http://www.relisoft.com/forums/style_images/1/post_snapback.gif" border=0></a></div>
<div class=quotemain><!--quotec-->That's right. It needs an IDE, tools, and libraries. But it has a good chance of acqiring them quickly.<!--quoteend--></div>
<!--quoteeend--><br><br>It would be great if there were a Visual Studio package for D <img style="VERTICAL-ALIGN: middle" alt=smile.gif src="http://www.relisoft.com/forums/style_emoticons/default/smile.gif" border=0 emoid=":)">.<br><br>Unfortunately after trying to figure out how to do that by<br>browsing the Visual Studio SKD documentation and samples<br>for the last two days I finally gave up <img style="VERTICAL-ALIGN: middle" alt=sad.gif src="http://www.relisoft.com/forums/style_emoticons/default/sad.gif" border=0 emoid=":(">.<br><br>There are two frameworks that are supposed to help<br>with the package development. One is written in C#<br>and the second is in C++ (depends heavily on Microsoft's ATL).<br>These are pretty heavy beasts and doesn't really<br>help if the developer doesn't get The Big Picture<br>(as I failed to get).<br><br>So, I tried to implement a package from scratch.<br><br>So far I get the IDE to show a new project type<br>and to call my implementation of the IVsProjectFactory<br>interface. When the IDE calls the<br>IVsProjectFactory::CreateProject I copy<br>the project template files into the new project<br>directory. Then I create an object implementating<br>the IVsProject3 and IVsHierarchy interfaces and<br>return it back to the IDE.<br>This is where I am stuck. Although the IDE then<br>calls some methods on the IVsProject3 and<br>IVsHierarchy interfaces, querying and setting<br>some properties. The IDE doesn't show the<br>new project node in the solution explorer.<br>There is only the topmost solution node<br>"Solution 'Project1' (1 Project)", nothing more.<br><br>And as I said before I was not able to figure out<br>what am I supposed to do after the project is created... <img style="VERTICAL-ALIGN: middle" alt=sad.gif src="http://www.relisoft.com/forums/style_emoticons/default/sad.gif" border=0 emoid=":("><br><br>~Peter <br><br><strong style="COLOR: #ff0000">Bartosz：</strong><br>
<div class=postcolor id=post-3305>The best resource in this kind of projects are Microsoft forums. Here's a thread about adding language services to VS<br><a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1072082&amp;SiteID=1" target=_blank><u>adding language services to VS </u></a>, and here's a <a href="http://forums.microsoft.com/MSDN/Search/Search.aspx?words=new+language+service&amp;localechoice=9&amp;SiteID=1&amp;searchscope=forumscope&amp;ForumID=57" target=_blank><u>more comprehensive list </u></a>. Without these forums I would have never been able to embed the internet browser control in Code Co-op.<br><br>Another-- maybe even more attractive--option is to write an extension to Eclipse. There's been <a href="http://www.digitalmars.com/d/archives/16689.html" target=_blank><u>some discussion </u></a>about it in the D forum, but it doesn't look like there are many volunteers. <!--ibf.attachment_3305--></div>
<br><strong style="COLOR: #ff0000">peter:</strong><br>
<div class=postcolor id=post-3322>Speaking about the D Programming Language.<br><br>How does it handle source file dependencies?<br><br>If I have two files File1.d and File2.d<br>and File2.d imports File1, then when<br>File1.d changes both files must be<br>recompiled.<br><br>Is my assumption correct?<br><br>~Peter <!--ibf.attachment_3322--></div>
<!-- THE POST --><br><strong style="COLOR: #ff0000">Bartosz:<br></strong>
<div class=postcolor id=post-3323>This is a functionality of "make", not the compiler. But if you mean: Should both files be recompiled?; the answer is, yes. <br><br>Note also that you can generate D interface files, .di, which speed up the import process. These too have to be recompiled whenever their correspondig D file changes. <!--ibf.attachment_3323--></div>
<!-- THE POST --><br><strong style="COLOR: #ff0000">peter:<br></strong>
<div class=postcolor id=post-3347>(Maybe you could add a new forum section about "D Programming" :-)<br><br>I wonder how one implements resource ownership transfer semantic in D?<br><br>~Peter <!--ibf.attachment_3347--></div>
<!-- THE POST --><br><strong style="COLOR: #ff0000">Bartosz:</strong><br>
<div class=quotetop>QUOTE (peter @ Feb 15 2007, 07:34 AM) <a href="http://www.relisoft.com/forums/index.php?act=findpost&amp;pid=3347"><img alt=* src="http://www.relisoft.com/forums/style_images/1/post_snapback.gif" border=0></a></div>
<div class=quotemain><!--quotec-->(Maybe you could add a new forum section about "D Programming" :-)<!--quoteend--></div>
<!--quoteeend--><br>I will do exactly that! <br><br><strong style="COLOR: #ff0000">helmi:<br></strong>
<div class=postcolor id=post-3353>It is a good idea. <!--ibf.attachment_3353--></div>
<!-- THE POST --><br><strong style="COLOR: #ff0000">James:</strong><br>
<div class=postcolor id=post-3380>I hope none of you mind me butting in for just a second.<br><br>I am currently a student in high school and I am interested in computer science and programming. I am fluent (on an intermediate level) in PHP (HTML, XML, and some minor AJAX/JS included). I would say I have a decent understanding of general coding practice/syntax/what have you, so I am certainly not starting from scratch.<br><br>In terms of D, C++, C#, Java, etc., where should I be focusing my efforts? I know there really isn't a straight forward answer, but maybe some of you could offer your opinion. I'm not necessarily looking for something thats going to land me the biggest paycheck (although I would be interested in what you have to say on the subject), but more something that will give me a good background for future endeavors, whether or not it be a new language.<br><br>I'm not sure if I can really go wrong, but I am just trying to find something that might suit me the best. I am not setting out to make anything in particular, like games or stuff along those lines. As said, I am just interest in furthering my understanding and setting myself up for down the road. Any tips you can offer me would be great.<br><br>Thanks<br><br>James <!--ibf.attachment_3380--></div>
<!-- THE POST --><br><strong style="COLOR: #ff0000">Bartosz:</strong><br>
<div class=quotetop>QUOTE (James @ Feb 20 2007, 02:02 PM) <a href="http://www.relisoft.com/forums/index.php?act=findpost&amp;pid=3380"><img alt=* src="http://www.relisoft.com/forums/style_images/1/post_snapback.gif" border=0></a></div>
<div class=quotemain><!--quotec-->In terms of D, C++, C#, Java, etc., where should I be focusing my efforts?<!--quoteend--></div>
<!--quoteeend-->These languages have very similar structure, so my suggestion would be to learn them all. In order of difficulty, from easiest to hardest:<br>- Java<br>- C#<br>- D<br>- C++<br>Specializing in one single language not only pidgenholes you as a developer, but also prevents you from learning different programming styles. <!--ibf.attachment_3389--><!-- THE POST --><br><strong style="COLOR: #ff0000">James:<br></strong>
<div class=postcolor id=post-3390>Thanks. I appreciate the reply. <!--ibf.attachment_3390--></div>
<!-- THE POST --><br><strong style="COLOR: #ff0000">Dave:<br></strong>
<div class=postcolor id=post-3811>I've programming in Java, C#, C++ and many other languages over many years.<br><br>I've never seen any commercial demand for D so that may well be a stumbling point. I would look very closely at what happened to Borland/CodeGear. Of course alot of unix based open source languages have done well so maybe i'm wrong.<br><br>Bjarne is working on his next version of C++ called C++ 0x, I doubt even this will get traction in many places.<br><br>Assembler, C and C++ were general purpose system level languages, things have moved on now, sure they are still valid for development of the OS, drivers, performance and memory footprint critical software, but they do not support many of the higher level primitives business software and web developers want.<br><br>Most programmers in the world however don't work on the large volume projects where the increased development costs can be amortised. They work on custom in house solutions to business problems, this is why we have Java and C#, they are the modern equivalent of Smalltalk that is now feasible due to vast hardware resources. These projects generally last only a few years before a rewrite making developer cost a large factor, and hardware cost fairly insignificant.<br><br>I would therefore strongly advise most people to learn at least one high level language to go with their lower level languages in order to have a successful career. Its unfortunate alot of the old skills are being lost in assembler, compiler design etc. I would still certainly reccomend most new students learning C# or Java over C/C++ or D as their first language.<br><br>Eclipse is an excellent open source IDE, It already supports multiple languages, and runs on multiple OS's it might be a better bet for D than Visual Studio. <!--ibf.attachment_3811--></div>
<!-- THE POST --><br><strong style="COLOR: #ff0000">ivec:<br></strong>
<div class=quotetop>QUOTE (Dave @ Aug 23 2007, 12:25 PM) <a href="http://www.relisoft.com/forums/index.php?act=findpost&amp;pid=3811"><img alt=* src="http://www.relisoft.com/forums/style_images/1/post_snapback.gif" border=0></a></div>
<div class=quotemain><!--quotec-->Bjarne is working on his next version of C++ called C++ 0x, I doubt even this will get traction in many places.<!--quoteend--></div>
<!--quoteeend--><br><br>This is just so inaccurate that I have to clarify:<br>This is not about "Bjarne working on his next version of his language".<br>It is the ISO C++ standard committee working on the next revision of the standard C++ language. This is a collaborative and active process, as can be seen at <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/" target=_blank><u>http://www.open-std.org/jtc1/sc22/wg21/docs/papers/</u></a> .<br><br>When the standard is adopted (expected in a couple of years), you can be certain that all vendors of C++ compilers will be implementing the features introduced in the new standard. I am very confident that the new standard will have no problem getting traction wherever C++ remains relevant. <!--ibf.attachment_3840--><br><br>--------------------<br>
<div class=signature><a href="http://ivan.vecerina.com/" target=_blank><u><font color=#222222>http://ivan.vecerina.com</font></u></a></div>
<!-- THE POST --><br><strong style="COLOR: #ff0000">Bartosz:<br></strong>
<div class=postcolor id=post-3844>Yes, a lot of bright people work on the development of C++, not only in the Standards Committee; and I expect C++ 0x to introduce a lot of great features. <br><br>There are however some aspects of language development that are not reachable from the current state of C++, because of backwards compatibility. That's where D will shine. Version 2.0 of D is expected about the same time as C++ 0x-- meaning, before 2010. (The 0x stands for 200x--C++ is cutting it real close!).<br><br>As an example, D will unify templates and function templates--In D a function is just a fully specialized template. <!--ibf.attachment_3844--></div>
<!-- THE POST --><br><strong style="COLOR: #ff0000">hansgostajonsson:<br></strong>
<div class=postcolor id=post-4113>Hello! As a hobby programmer I am hardly qualified to write here but: When I studied programming at university in 1987 I learnt pascal and loved it. It was said to be the future, but "for old times sake" we were also required to learn cobol and fortran - 2 languages then thought to be barely breathing. They seem , however, still to be hanging around, pascal is still in swing as Delphi but it was not so long after I learnt it said to be decrepit and I had to move on to c and then c++ if I wanted to move with the times. Then Java was the swinging language and so on to c#. Now D looms high on the horizon. It may seem a naive question but why can't d be implemented as a development of c++ - to an amateur it seems a look alike. hans j&#246;nsson <!--ibf.attachment_4113--></div>
<!-- THE POST --><br><strong style="COLOR: #ff0000">Bartosz:<br></strong>
<div class=postcolor id=post-4114>If you look at the development of C++ over the years you'll notice that virtually every feature that's been added to the language stays with it, whether it made sense or not. If you imagine C as a fast but flimsy and dangerous ship, C++ grew layers and layers of barnacles over it, in the hope to strengthening it. Instead C++ has kept all the weaknesses of C at its core; plus it made if very difficult not only to master, but even to start learning.<br><br>The direction of D that I'm advocating is to make it as easy and safe for beginners as Java or C#. You should be able to use D productively without the need to understand pointers or memory management (D is garbage-collected). D has built in (and very efficient) arrays and strings. Classes are treated pretty much like in Java. <br><br>But unlike Java, D also offers all the sophisticated features of C++, except that they were desingned into the language from the start. Instead of layers of barnacles you have some well designed solid language architecture. <!--ibf.attachment_4114--></div>
<!-- THE POST --><br><strong style="COLOR: #ff0000">peter:<br></strong>
<div class=postcolor id=post-4115>Personally, I don't believe that Yet Another Programming Language Based on C/C++, Java, etc. will make our coding and code maintaining life any easier. Programming languages evolved over 50 years yet I think the most radical step to writing more correct and more maintainable software was that from FORTRAN to ALGOL. From that point on any new language (from the ALGOL family) brings IMHO only very marginal improvements wrt. code maintainability and I don't think this will change in the future. Also after that 50 year of programming evolution and experience our best programming tool is still a simple text editor. I don't think this is right.<br><br>Presently, I'm slowly turning my attention to other programming paradigms. From them I see the Data Flow and Multi Agent paradigms most promising partly due to their great potential to exploit the future many-core processors. Also they lend themselves more easily to "diagrammatic" and/or visual programming than present day mainstream programming languages.<br><br>Just my €0.02<br><br>~Peter<br><br>P.S. I sometimes browse through the digitalmars.D newsserver and, frankly, based on most posts there the very LAST thing I'd say about the D programming language is: "well designed solid language architecture".<br><br>Also, I looked at the sources for the D compiler and the first immediate thought was: What programming language can design a person with such coding practices... I mean no offense to Walter Bright (I really admire his persistence and devotion to his creation), but those were just my feelings. <!--ibf.attachment_4115--></div>
<!-- THE POST --><br><strong style="COLOR: #ff0000">hansgostajonsson:</strong><br>
<div class=postcolor id=post-4116>Thanks for replies. A few musings. A new language, like reorganizations, give new hope an generally we learn something in the process. I am a newly retired psychologist with old studies in computer science. Psychology differs from other sciences - it is one in many of it's fields - in that it has been founded a couple of times ( Gestalt psychology, functionalism, behaviourism) - not once like most other sciences. It seems to be the same for language development in computer science and you long for something like the explanation of dna when genetics became molecular biology and never has looked back again. Parhaps a futile dream. It seems that d is based on practical considerations rather than theoretical progress and therefore will be superseeded by the next language but not killed off. I think one problem is that new languages are not good enough to kill once and for all older languages. We will have a lot of not so good but good enough ones hanging around - the same in psychology. Just a few musings. Thanks for a very interesting site. greetings hans j&#246;nsson <!--ibf.attachment_4116--></div>
<!-- THE POST --><br><!--ibf.attachment_3349--><!-- THE POST --><strong><span style="COLOR: #ff0000">Bartosz:<br></span></strong>
<div class=quotetop>QUOTE (peter @ Mar 4 2008, 05:22 AM) <a href="http://www.relisoft.com/forums/index.php?act=findpost&amp;pid=4115"><img alt=* src="http://www.relisoft.com/forums/style_images/1/post_snapback.gif" border=0></a></div>
<div class=quotemain><!--quotec-->Personally, I don't believe that Yet Another Programming Language Based on C/C++, Java, etc. will make our coding and code maintaining life any easier. Programming languages evolved over 50 years yet I think the most radical step to writing more correct and more maintainable software was that from FORTRAN to ALGOL. From that point on any new language (from the ALGOL family) brings IMHO only very marginal improvements wrt. code maintainability and I don't think this will change in the future.<!--quoteend--></div>
<!--quoteeend--><br>A lot happend since ALGOL. Probably the biggest revolution was the introduction of the object oriented paradigm. OO programs are a lot more maintainable. In other areas progress was more quantitative than qualitative, but the accumulation of changes can make a lot of difference. Garbage collection, for instance, has entered the mainstream. So did functional programming techniques. There was also a shift of emphasis towards safe programming. Not to mention the latest revolution--multicore. Java bit the bullet by defining a memory model for parallel programming. C++ is working on it too. <br><!--quoteo-->
<div class=quotetop>QUOTE </div>
<div class=quotemain><!--quotec-->Also after that 50 year of programming evolution and experience our best programming tool is still a simple text editor. I don't think this is right.<!--quoteend--></div>
<!--quoteeend-->No, it's not right. Personally, I use Visual Studio in my C++ development. I also used Eclipse for Java. C++ has some of the worst development tools because it's such a hard language to parse. By the way, there is a D plugin for Eclipse. I didn't use it because it doesn't support D 2.0 yet.<br><!--quoteo-->
<div class=quotetop>QUOTE </div>
<div class=quotemain><!--quotec-->Presently, I'm slowly turning my attention to other programming paradigms. From them I see the Data Flow and Multi Agent paradigms most promising partly due to their great potential to exploit the future many-core processors. Also they lend themselves more easily to "diagrammatic" and/or visual programming than present day mainstream programming languages.<!--quoteend--></div>
<!--quoteeend-->These are all cool paradigms, but they are still far from the mainstream. My current interest is in Software Transactional Memory as the new paradigm for multicore programming.<br><!--quoteo-->
<div class=quotetop>QUOTE </div>
<div class=quotemain><!--quotec-->P.S. I sometimes browse through the digitalmars.D newsserver and, frankly, based on most posts there the very LAST thing I'd say about the D programming language is: "well designed solid language architecture".<!--quoteend--></div>
<!--quoteeend-->Point taken. Version 2.0 is still in flux. It might look like there is a lot of random experimenting, but there is solid theory behind most of it. In fact I'm pushing for updating the D manifesto to better explain the goals of the language. There is also a big difference between version 1.0 and 2.0. Version 1.0 was more of a "better C++". 2.0 is way beyond that. <br><!--quoteo-->
<div class=quotetop>QUOTE </div>
<div class=quotemain><!--quotec-->Also, I looked at the sources for the D compiler and the first immediate thought was: What programming language can design a person with such coding practices... I mean no offense to Walter Bright (I really admire his persistence and devotion to his creation), but those were just my feelings.<!--quoteend--></div>
<!--quoteeend--><br>Touche! What can I say, I'm not fond of Walter's programming style either <img style="VERTICAL-ALIGN: middle" alt=wink.gif src="http://www.relisoft.com/forums/style_emoticons/default/wink.gif" border=0 emoid=";)">. <br><br><strong style="COLOR: #ff0000">Bartosz:</strong><br>
<div class=quotetop>QUOTE (hansgostajonsson @ Mar 4 2008, 12:51 PM) <a href="http://www.relisoft.com/forums/index.php?act=findpost&amp;pid=4116"><img alt=* src="http://www.relisoft.com/forums/style_images/1/post_snapback.gif" border=0></a></div>
<div class=quotemain><!--quotec-->It seems that d is based on practical considerations rather than theoretical progress<!--quoteend--></div>
<!--quoteeend--><br>Practical considerations make or break a language. There are some theoretically beautiful languages that nobody uses outside of the academia because they are not practical.<br><br>But there's also a lot of theoretical progress in computer science that is being incorporated into D. <br><br><strong style="COLOR: #ff0000">Hossein:</strong><br>
<div class=quotetop>QUOTE (Bartosz @ Mar 4 2008, 11:10 PM) <a href="http://www.relisoft.com/forums/index.php?act=findpost&amp;pid=4118"><img alt=* src="http://www.relisoft.com/forums/style_images/1/post_snapback.gif" border=0></a></div>
<div class=quotemain><!--quotec-->But there's also a lot of theoretical progress in computer science that is being incorporated into D.<!--quoteend--></div>
<!--quoteeend--><br><br>Wow! This is what amused me in the first place in fact! <img style="VERTICAL-ALIGN: middle" alt=biggrin.gif src="http://www.relisoft.com/forums/style_emoticons/default/biggrin.gif" border=0 emoid=":D"> OK, it seems that I can deny my extreme laziness here, and ask for this from you (Bartosz): Could you please give a list of all these nice features of D which attract us -- the snubish [<img style="VERTICAL-ALIGN: middle" alt=wink.gif src="http://www.relisoft.com/forums/style_emoticons/default/wink.gif" border=0 emoid=";)">] people of Theoretical Computer Science of academia?<br><br>(And, BTW, like I mentioned in some other posting: People of academia wouldn't really like it that they don't publish proceedings in their D conferences...)<br><br>Cheers,<br>--Hossein <br><br><strong style="COLOR: #ff0000">Bartosz:<br></strong>
<div class=postcolor id=post-4127>We are trying to incorporate the latest PL trends in D, sometimes even before they mature <img style="VERTICAL-ALIGN: middle" alt=wink.gif src="http://www.relisoft.com/forums/style_emoticons/default/wink.gif" border=0 emoid=";)">. <br><br>One major area is safe programming. There's been a lot of research into proving soundness of various languages using operational semantics. It culminated in the soundness proof for Featherweight (Generic) Java. Although it's not a goal of D to be a sound language (we do want pointers after all), we are defining a sound subset of D. In particular, we have a program that can translate Java programs into this subset. We will have one-to-one correspondence between Java semantics and the semantics of a subset of D (which is equivalent to having denotational semantics for the D subset). <br><br>We are planning on extending the safe D subset with other sound features, such as discriminated unions and elements of functional programming. D already supports anonymous functions and closures. <br><br>The other major area is concurrent programming. Again, Java leads the crowd with its memory model. D's memory model will most likely resemble that of C++ (still in the form of a proposal). The difference between the Java model and the C++ model is that Java gives some guarantees even for programs with race conditions-- C++ doesn't. So a racy program in C++ may exhibit undefined behavior. On the other hand Java has a virtual machine that may enforce a lot more during runtime that can be accomplished in a compiled language.<br><br>How can we support safe concurrency in the presence of races--or eliminate the races altogether? The idea is to build software transactional memory into D. Even though STM might not offer the best performance, it is much safer (and easier) than lock-based programming. Especially if we can build support for transactions into the type system, as has been done for Concurrent Haskell. <!--ibf.attachment_4127--></div>
<!-- THE POST --><br><strong style="COLOR: #ff0000">markm:<br></strong>
<div class=postcolor id=post-4132>IDE for D use CodeBlocks, version 8.02 supports D.<br><br><a href="http://www.codeblocks.org/" target=_blank><u>http://www.codeblocks.org/</u></a> <!--ibf.attachment_4132--></div>
<!-- THE POST --><br><br><br><!--ibf.attachment_4125--><!-- THE POST --><br><!--ibf.attachment_4118--><!-- THE POST --><br><!--ibf.attachment_4117--><!-- THE POST --><br><br><br><!--ibf.attachment_3303--><!-- THE POST --><br><br><!--ibf.attachment_3303--><!-- THE POST --><br><!--ibf.attachment_3296--><!-- THE POST --><br><!-- THE POST -->
<img src ="http://www.cppblog.com/xiaoluoluo/aggbug/102021.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xiaoluoluo/" target="_blank">小罗罗</a> 2009-11-27 00:23 <a href="http://www.cppblog.com/xiaoluoluo/archive/2009/11/27/102021.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>散列2</title><link>http://www.cppblog.com/xiaoluoluo/archive/2009/11/26/102001.html</link><dc:creator>小罗罗</dc:creator><author>小罗罗</author><pubDate>Thu, 26 Nov 2009 12:20:00 GMT</pubDate><guid>http://www.cppblog.com/xiaoluoluo/archive/2009/11/26/102001.html</guid><wfw:comment>http://www.cppblog.com/xiaoluoluo/comments/102001.html</wfw:comment><comments>http://www.cppblog.com/xiaoluoluo/archive/2009/11/26/102001.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xiaoluoluo/comments/commentRss/102001.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xiaoluoluo/services/trackbacks/102001.html</trackback:ping><description><![CDATA[<p><font style="FONT-SIZE: 12pt; BACKGROUND-COLOR: #cde7d0">3 不使用链表的散列表<br>&nbsp;&nbsp;&nbsp; 分离链接散列算法的缺点是使用一些链表。由于给新单元分配地址需要时间，因此这就导致算法的速度有些缓慢，同时算法实际上还要求第二种数据结构的实现。解决冲突的另一个方法是当冲突发生时就尝试选择另一个单元，直到找到空的单元。更正式地，单元h<sub>i</sub>(x)=(hash(x)+f(i))mod TableSize,且f(0)=0.函数f是冲突解决函数。因为所有的数据都要置入表内，所以使用这个方案所需要的表要比分离链接散列需要的表大。一般说来，对不使用分离链接法的散列表来说，其装填因子应该低于&#955;=0.5。我们称这样的表为探测散列表(probing hash tables)。<br>&nbsp;&nbsp;&nbsp; (1)当f是i的线性函数时，为线性探测，一般情况下f(i)=i，线性探测容易在占据的单元形成一些区块，其结果成为一次聚集(primary clustering)。<br>&nbsp;&nbsp;&nbsp;&nbsp;(2)平方探测是消除线性探测中一次聚集问题的冲突解决方法。平方探测就是冲突函数为二次函数的探测方法。流行的选择是f(i)=i<sup>2</sup>.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>定理</strong>：如果使用平方探测，且表的大小是素数，那么当表至少有一半是空的时候，总能够插入一个新的元素。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 如果哪怕表有比一半多一个的位置被填满，那么插入都有可能失败(虽然这种可能性极小)。另外，表的大小是素数也非常重要。如果表的大小不是素数，则备选单元<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;的个数可能会锐减。例如，若表的大小是16，那么备选单元只能在距散列值1，4或9远处。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="COLOR: #0000ff">在探测散列表中标准的删除操作不能执行，因为相应的单元可能已经引起过冲突，元素绕过它存储在别处。因此，探测散列表需要懒惰删除。</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 实现探测散列表所需要的类接口在下图中给出。这里不使用链表数组，而是使用散列表项单元数组。嵌套的类HashEntry存储在info成员中一个项的状态，这个状态可<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;以是ACTIVE,EMPTY或DELETED。<br><br>&nbsp;&nbsp;&nbsp; </p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: small; 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: #008000">//</span><span style="COLOR: #008000">使用探测策略的散列表的类接口，包括嵌套的HashEntry</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;类<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>template&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">typename&nbsp;HashedObj</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><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></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;HashTable<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img id=Codehighlighter1_82_741_Open_Image onclick="this.style.display='none'; Codehighlighter1_82_741_Open_Text.style.display='none'; Codehighlighter1_82_741_Closed_Image.style.display='inline'; Codehighlighter1_82_741_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_82_741_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_82_741_Closed_Text.style.display='none'; Codehighlighter1_82_741_Open_Image.style.display='inline'; Codehighlighter1_82_741_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_82_741_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_82_741_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></span><span style="COLOR: #0000ff">public</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/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">explicit</span><span style="COLOR: #000000">&nbsp;HashTable(&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;size&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">101</span><span style="COLOR: #000000">&nbsp;);<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>&nbsp;&nbsp;&nbsp;&nbsp;<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;</span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;contains(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;HashedObj&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">x&nbsp;)&nbsp;</span><span style="COLOR: #0000ff">const</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>&nbsp;&nbsp;&nbsp;&nbsp;<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;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;makeEmpty();<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;insert(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;HashedObj&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">x&nbsp;);<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;</span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;remove(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;HashedObj&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">x&nbsp;);<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;&nbsp;&nbsp;&nbsp;<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;emum&nbsp;EntryType&nbsp;(ACTIVE,EMPTY,DELETED&nbsp;);<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></span><span style="COLOR: #0000ff">private</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>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;HashEntry<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img id=Codehighlighter1_375_534_Open_Image onclick="this.style.display='none'; Codehighlighter1_375_534_Open_Text.style.display='none'; Codehighlighter1_375_534_Closed_Image.style.display='inline'; Codehighlighter1_375_534_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_375_534_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_375_534_Closed_Text.style.display='none'; Codehighlighter1_375_534_Open_Image.style.display='inline'; Codehighlighter1_375_534_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_375_534_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_375_534_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;HashedObj&nbsp;element;<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;EntryType&nbsp;info;<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img id=Codehighlighter1_526_528_Open_Image onclick="this.style.display='none'; Codehighlighter1_526_528_Open_Text.style.display='none'; Codehighlighter1_526_528_Closed_Image.style.display='inline'; Codehighlighter1_526_528_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_526_528_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_526_528_Closed_Text.style.display='none'; Codehighlighter1_526_528_Open_Image.style.display='inline'; Codehighlighter1_526_528_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HashEntry(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;HashedObj&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;e&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;HashedObj(),&nbsp;EntryType&nbsp;i&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;EMPTY&nbsp;)&nbsp;:&nbsp;element(e),&nbsp;info(i)&nbsp;</span><span id=Codehighlighter1_526_528_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_526_528_Open_Text><span style="COLOR: #000000">{&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></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/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">HashEntry</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;array;<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;currentSize;<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;<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;isActive(&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;currentPos&nbsp;)&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;findPos(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;HashedObj&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">x&nbsp;)&nbsp;</span><span style="COLOR: #0000ff">const</span><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>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;rehash();<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;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;myhash(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;HashedObj&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">x&nbsp;)&nbsp;</span><span style="COLOR: #0000ff">const</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/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000">;</span></div>
<p><br>&nbsp;</p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: small; 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: #008000">//</span><span style="COLOR: #008000">初始化平方探测散列表的例程</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">explicit</span><span style="COLOR: #000000">&nbsp;HashTable(&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;size&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">101</span><span style="COLOR: #000000">&nbsp;)&nbsp;:&nbsp;array(nextPrime(&nbsp;size&nbsp;)&nbsp;)<br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img id=Codehighlighter1_81_96_Open_Image onclick="this.style.display='none'; Codehighlighter1_81_96_Open_Text.style.display='none'; Codehighlighter1_81_96_Closed_Image.style.display='inline'; Codehighlighter1_81_96_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_81_96_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_81_96_Closed_Text.style.display='none'; Codehighlighter1_81_96_Open_Image.style.display='inline'; Codehighlighter1_81_96_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_81_96_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_81_96_Open_Text><span style="COLOR: #000000">{&nbsp;makeEmpty();&nbsp;}</span></span><span style="COLOR: #000000"><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></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;makeEmpty()<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img id=Codehighlighter1_116_212_Open_Image onclick="this.style.display='none'; Codehighlighter1_116_212_Open_Text.style.display='none'; Codehighlighter1_116_212_Closed_Image.style.display='inline'; Codehighlighter1_116_212_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_116_212_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_116_212_Closed_Text.style.display='none'; Codehighlighter1_116_212_Open_Image.style.display='inline'; Codehighlighter1_116_212_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_116_212_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_116_212_Open_Text><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/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;currentSize&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">&nbsp;8</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&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</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">array.size();&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">&nbsp;)<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;&nbsp;&nbsp;&nbsp;array[i].info&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;EMPTY;<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div>
<p><br><br>&nbsp;</p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: small; 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: #008000">//</span><span style="COLOR: #008000">使用平方探测进行散列的contains例程</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;contains(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;HashedObj&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">x&nbsp;)&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img id=Codehighlighter1_66_103_Open_Image onclick="this.style.display='none'; Codehighlighter1_66_103_Open_Text.style.display='none'; Codehighlighter1_66_103_Closed_Image.style.display='inline'; Codehighlighter1_66_103_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_66_103_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_66_103_Closed_Text.style.display='none'; Codehighlighter1_66_103_Open_Image.style.display='inline'; Codehighlighter1_66_103_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_66_103_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_66_103_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;isActive(&nbsp;findPos(x)&nbsp;);&nbsp;}</span></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><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">int</span><span style="COLOR: #000000">&nbsp;findPos(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;HashedObj&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">x&nbsp;)&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img id=Codehighlighter1_146_477_Open_Image onclick="this.style.display='none'; Codehighlighter1_146_477_Open_Text.style.display='none'; Codehighlighter1_146_477_Closed_Image.style.display='inline'; Codehighlighter1_146_477_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_146_477_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_146_477_Closed_Text.style.display='none'; Codehighlighter1_146_477_Open_Image.style.display='inline'; Codehighlighter1_146_477_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_146_477_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_146_477_Open_Text><span style="COLOR: #000000">{<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>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;offset&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">&nbsp;9</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;currentPos&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;myhash(x);<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;<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;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">下面是一个小小的trick</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">12</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;(&nbsp;array[&nbsp;currentPos&nbsp;].info&nbsp;</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">&nbsp;EMPTY&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;array[&nbsp;currentPos&nbsp;].element&nbsp;</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">&nbsp;x&nbsp;)<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img id=Codehighlighter1_313_447_Open_Image onclick="this.style.display='none'; Codehighlighter1_313_447_Open_Text.style.display='none'; Codehighlighter1_313_447_Closed_Image.style.display='inline'; Codehighlighter1_313_447_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_313_447_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_313_447_Closed_Text.style.display='none'; Codehighlighter1_313_447_Open_Image.style.display='inline'; Codehighlighter1_313_447_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_313_447_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_313_447_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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;currentPos&nbsp;</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">&nbsp;offset;<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;offset&nbsp;</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">2</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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;currentPos&nbsp;</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">&nbsp;array.size()&nbsp;)<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;&nbsp;&nbsp;&nbsp;currentPos&nbsp;</span><span style="COLOR: #000000">-=</span><span style="COLOR: #000000">&nbsp;array.size();<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><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;<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;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;currentPos;<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: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;isActive(&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;currentPos&nbsp;)&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img id=Codehighlighter1_518_567_Open_Image onclick="this.style.display='none'; Codehighlighter1_518_567_Open_Text.style.display='none'; Codehighlighter1_518_567_Closed_Image.style.display='inline'; Codehighlighter1_518_567_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_518_567_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_518_567_Closed_Text.style.display='none'; Codehighlighter1_518_567_Open_Image.style.display='inline'; Codehighlighter1_518_567_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_518_567_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_518_567_Open_Text><span style="COLOR: #000000">{<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;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;array[&nbsp;currentPos&nbsp;].info&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;ACTIVE;&nbsp;}</span></span></div>
<p><br><br>&nbsp;</p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: small; 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: #008000">//</span><span style="COLOR: #008000">使用平方探测的散列表的insert和remove例程</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;insert&nbsp;(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;HashedObj&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">x&nbsp;)<br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img id=Codehighlighter1_64_296_Open_Image onclick="this.style.display='none'; Codehighlighter1_64_296_Open_Text.style.display='none'; Codehighlighter1_64_296_Closed_Image.style.display='inline'; Codehighlighter1_64_296_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_64_296_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_64_296_Closed_Text.style.display='none'; Codehighlighter1_64_296_Open_Image.style.display='inline'; Codehighlighter1_64_296_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_64_296_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_64_296_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;currentPos&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;findPos(&nbsp;x&nbsp;);<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(&nbsp;isActive&nbsp;(&nbsp;currentPos&nbsp;)&nbsp;)<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">false</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/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;array[&nbsp;currentPos&nbsp;]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;hashEntry(&nbsp;x,&nbsp;ACTIVE&nbsp;);<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;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">currentSize&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;array.size()&nbsp;</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">&nbsp;)<br></span><span style="COLOR: #008080">10</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;rehash();<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;<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">13</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">14</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;remove(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;HashedObj&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">x&nbsp;)<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img id=Codehighlighter1_333_497_Open_Image onclick="this.style.display='none'; Codehighlighter1_333_497_Open_Text.style.display='none'; Codehighlighter1_333_497_Closed_Image.style.display='inline'; Codehighlighter1_333_497_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_333_497_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_333_497_Closed_Text.style.display='none'; Codehighlighter1_333_497_Open_Image.style.display='inline'; Codehighlighter1_333_497_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_333_497_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_333_497_Open_Text><span style="COLOR: #000000">{<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;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;currentPos&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;findPos(x);<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;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(&nbsp;</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">isActive(&nbsp;currentPos&nbsp;)&nbsp;)<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;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">false</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>&nbsp;&nbsp;&nbsp;&nbsp;<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;array[&nbsp;currentPos&nbsp;].info&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;DELETED;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">传说中的懒惰删除</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">22</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div>
<p><br>&nbsp;&nbsp;(3)最后一个冲突解决方法是双散列(double&nbsp;hashing)。对于双散列，一种流行的选择是f(i)=i*hash<sub>2</sub>(x)。这个公式是说，将第二个散列函数应用到x并在距离hash<sub>2</sub>(x),2hash<sub>2</sub>(x),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...等处探测。hash<sub>2</sub>(x)选择不好将会非常糟糕。<br>&nbsp;&nbsp;&nbsp; <br>.</font></p>
<img src ="http://www.cppblog.com/xiaoluoluo/aggbug/102001.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xiaoluoluo/" target="_blank">小罗罗</a> 2009-11-26 20:20 <a href="http://www.cppblog.com/xiaoluoluo/archive/2009/11/26/102001.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>数据结构-散列1</title><link>http://www.cppblog.com/xiaoluoluo/archive/2009/11/25/101935.html</link><dc:creator>小罗罗</dc:creator><author>小罗罗</author><pubDate>Wed, 25 Nov 2009 14:22:00 GMT</pubDate><guid>http://www.cppblog.com/xiaoluoluo/archive/2009/11/25/101935.html</guid><wfw:comment>http://www.cppblog.com/xiaoluoluo/comments/101935.html</wfw:comment><comments>http://www.cppblog.com/xiaoluoluo/archive/2009/11/25/101935.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xiaoluoluo/comments/commentRss/101935.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xiaoluoluo/services/trackbacks/101935.html</trackback:ping><description><![CDATA[<p><font style="FONT-SIZE: 14pt; BACKGROUND-COLOR: #cde7d0">&nbsp;&nbsp;&nbsp; 最近在看Bartosz Milewski的C++ In Action Industrial-Strength Programming Techniques，这本书很不错，它不是语言基础教程，全书主要介绍了许多工业强度的编程技术，如清理，隐藏实现细节，资源管理，共享，资源管理，使用标准模板库，重载运算符等技术。这是Bartosz Milewski的简介:<span style="FONT-SIZE: 14pt">Bartosz Milewski，C++ In Action 的作者。是Reliable Software公司的总裁，Reliable Software公司是一家为程序员制造高质量开发工具的公司。在过去几年间，Bartosz Milewski在多家知名杂志发表了大量技术文章。在微软工程工作的8年期间，他担任Windows2000中Content Index组件的开发主管，他曾经在波兰的Wroclaw大学讲授C++编程课程，而且他获得了Wroclaw大学的理论物理学博士学位。</span> 这是他为这本书管理的blog:http://www.relisoft.com/forums/index.php?s=ee4c0dc7cafef9f08c7c18a6e449b424&amp;showforum=3，售后服务还不错，呵呵，以我目前的水平看起来还挺累的，我想最好把C++ Primer和effective c++,more effective c++看完再看这本书会比较好，不过硬着头皮看也有好处，就像有时候武侠小说里用非常规方法提高修为有时候也能起到意想不到的效果。言归正传，在这本书中多次用到哈希表，很多地方不明白，查了书整理一下。<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 散列表(hash table)的实现常称为散列(hashing),散列是一种用于以常数平均时间执行插入，删除和查找的技术。但是，那些需要元素间任何排序信息的树操作不会得到有效的支持。因此诸如findMin,findMax以及在线性时间内按顺序打印整个表的操作都是散列所不支持的。<br>&nbsp;&nbsp;&nbsp; 我们把表的大小记作TableSize,我们把每个键映射到从0到TableSize-1这个范围中的某个数，并且将其放到适当的单元中。这个映射就称为散列函数(hash function),理想情况下它应该运算简单并且应该保证任何两个不同的键映射到不同的单元。不过，这是不可能的。因此我们寻找一个散列函数，该函数要在单元之间均匀分配键。这就是散列的基本思想，剩下的问题则是要选择一个函数，决定当两个键散列到同一个值的时候(称为(collision))应该做什么以及如何确定散列表的大小。<br><br>1.散列函数<br>&nbsp;&nbsp;&nbsp; 如果输入的键是整数，一般合理的方法就是直接返回"Key mod Tablesize",但Key具有某些不理想的性质，比如若表的大小是10而键的各位都是0。则此时上述标准的散列函数就不是一个好的选择，好的办法通常是保证表的大小是素数，当输入的键是随机整数时，散列函数不仅运算简单而且键的分配也很均匀。<br>&nbsp;&nbsp;&nbsp; 通常，键是字符串；在这种情形下，散列函数需要仔细选择。<br>&nbsp;&nbsp;&nbsp; 一种选择方法是把字符串中字符的ASCII码值加起来，下面的例程实现了这种策略。<br>&nbsp;&nbsp;&nbsp; </p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: small; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; HEIGHT: 135px; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">1</span><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;hash(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">key,&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;tableSize&nbsp;)<br></span><span style="COLOR: #008080">2</span><span style="COLOR: #000000"><img id=Codehighlighter1_45_167_Open_Image style="DISPLAY: inline" onclick="this.style.display='none'; Codehighlighter1_45_167_Open_Text.style.display='none'; Codehighlighter1_45_167_Closed_Image.style.display='inline'; Codehighlighter1_45_167_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top></span><span id=Codehighlighter1_45_167_Open_Text style="DISPLAY: inline"><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">3</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;hashVal&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">4</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&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</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">key.length();&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">&nbsp;)<br></span><span style="COLOR: #008080">5</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;hashVal&nbsp;</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">&nbsp;key[i];<br></span><span style="COLOR: #008080">6</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;hashVal&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;tableSize;<br></span><span style="COLOR: #008080">7</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div>
<p>&nbsp;&nbsp;&nbsp; 上述散列函数实现起来简单而且能够很快地算出答案。不过如果表很大，则函数就不会很好地分配键。例如，设TableSize=10007(10007是素数)，并设所有的键至多8个字符长，由于ASCII字符的值最多是127，因此散列函数只能在0-1016之间取值，显然这不是一种均匀的分配。<br>&nbsp;&nbsp;&nbsp; 另一个散列函数如下表示，这个散列函数假设Key至少三个字符。值27表示英文字母表的字母个数外加一个空格，而729是27&#215;27，该函数只考察前三个字符，但是如果它们是随机的，而表的大小还是10007，那么我们就会得到一个合理的均衡分布。可是，英文不是随机的。虽然3个字符（忽略空格）有26&#215;26&#215;26=17576种可能的组合，但查验词汇量足够大的联机词典却揭示出，3个字母的不同组合数实际上只有2851。即使这些组合没有冲突，也不过只有表的28%被真正散列到。因此，虽然很容易计算，但是当散列表足够大的时候这个函数还是不合适的。<br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: small; 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: #0000ff">int</span><span style="COLOR: #000000">&nbsp;hash(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">key,&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;tableSize&nbsp;)<br></span><span style="COLOR: #008080">2</span><span style="COLOR: #000000"><img id=Codehighlighter1_45_100_Open_Image onclick="this.style.display='none'; Codehighlighter1_45_100_Open_Text.style.display='none'; Codehighlighter1_45_100_Closed_Image.style.display='inline'; Codehighlighter1_45_100_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top></span><span id=Codehighlighter1_45_100_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">3</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;(key[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">27</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">key[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">729</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">key[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">])&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;tableSize<br></span><span style="COLOR: #008080">4</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div>
<p><br>&nbsp;&nbsp;&nbsp; 下面的例程列出了第3种尝试。这个散列函数涉及键中的所有字符，并且一般可以分布得很好，程序根据Horner法则计算一个37的多项式函数。这个散列函数利用了允许溢出这个事实。这可能会引进负数，因此在末尾有附加的测试，这个散列函数就表的分布而言未必是最好的，但是确实具有极其简单的优点而且速度也很快。如果键特别长，那么该散列函数计算起来将会花费过多的时间。在这种情况下通常做法是不使用所有的字符。此时键的长度和性质将影响选择。例如，键可能是完整的街道地址，散列函数可以包括街道地址的几个字符，或许也包括城市名和邮政编码的几个字符。有些程序设计人员通过只使用奇数位置上的字符来实现他们的散列函数，这里有这么一层想法：用计算散列函数节省下的时间来补偿由此产生的对均匀分布的函数的轻微干扰。<br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: small; 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: #0000ff">int</span><span style="COLOR: #000000">&nbsp;hash(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">key,&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;tableSize&nbsp;)<br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img id=Codehighlighter1_45_242_Open_Image onclick="this.style.display='none'; Codehighlighter1_45_242_Open_Text.style.display='none'; Codehighlighter1_45_242_Closed_Image.style.display='inline'; Codehighlighter1_45_242_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top></span><span id=Codehighlighter1_45_242_Open_Text><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/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;hashVal&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">&nbsp;4</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">key.length();i</span><span style="COLOR: #000000">++</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/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hashVal&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">37</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;hashVal&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;key[i];<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>&nbsp;&nbsp;&nbsp;&nbsp;hashVal&nbsp;</span><span style="COLOR: #000000">%=</span><span style="COLOR: #000000">&nbsp;tableSize;<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;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;hashVal&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">&nbsp;)<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hashVal&nbsp;</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">&nbsp;tableSize;<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;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;hashVal;<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div>
<p>&nbsp;&nbsp;&nbsp; 剩下的主要编程细节是解决冲突。如果当一个元素在插入时与一个已经插入的元素散列到相同的值，那么就产生一个冲突，这个冲突需要消除。<br><br><br><br>2.分离链接法<br>&nbsp;&nbsp;&nbsp; 解决冲突的第一种方法通常称为分离链接法(separate chaining),其做法是将散列到同一个值的所有元素保留到一个链表中，可以使用标准库中表的实现方法。如果空间很紧，则更可取的方法是避免使用它们(因为这些表是双向链表，浪费空间),为执行search,我们使用散列函数来确定究竟该遍历那个链表，然后查找相应的表。为执行insert，我们检查相应的表来确定是否该元素已经在表中了(如果要插入重复元，那么通常要留出一个额外的数据成员，当出现匹配事件时这个数据成员增1),如果这个元素是个新的元素，那么它将被插入到表的前端，这不仅因为方便，而且还因为最后插入的元素最有可能不久再次被访问，实现分离链接法所需要的类架构在下面的例程中给出。散列表结构存储一个链表数组，这个数组在构造函数中指定。<br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: small; 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: #008000">//</span><span style="COLOR: #008000">分离链接散列表的类构架</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #000000">template&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">typename&nbsp;HashedObj</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;3&nbsp; </span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;HashTable<br></span><span style="COLOR: #008080">&nbsp;4 </span><span id=Codehighlighter1_60_455_Open_Text><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/InBlock.gif" align=top></span><span style="COLOR: #0000ff">public</span><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>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">explicit</span><span style="COLOR: #000000">&nbsp;HashTable(&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;size&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">101</span><span style="COLOR: #000000">&nbsp;);<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><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>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;contains(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;HashedObj&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">x&nbsp;)&nbsp;</span><span style="COLOR: #0000ff">const</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/InBlock.gif" align=top><br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;makeEmpty();<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;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;insert(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;HashedObj&nbsp;x);<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;remove(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;HashedObj&nbsp;x);<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">14 </span><span style="COLOR: #0000ff">private</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;&nbsp;&nbsp;&nbsp;vector</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">list</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">HashedObj</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;theLists;// &gt;&gt;是c++的一个运算符，而且比&gt;长，因此两个&gt;之间需要一个空格<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;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;currentSize;<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;<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;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;rehash()<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;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;myhash(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;HashedObj&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">x&nbsp;)&nbsp;</span><span style="COLOR: #0000ff">const</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><br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;hash(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">key&nbsp;);<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;hash(&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;key&nbsp;);<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/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span></div>
<p>&nbsp;&nbsp;&nbsp; 这里不使用那些将对象和类的大小作为参数的散列函数，而是使用那些仅以对象为参数的散列函数，并且返回int。散列表类有一个私有的成员函数myhash,这个函数将结果分配到一个合适的数组索引中。</p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: small; 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: #0000ff">int</span><span style="COLOR: #000000">&nbsp;myhash(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;HashedObj&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">x&nbsp;)&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span id=Codehighlighter1_39_176_Open_Text><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/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;hashVal&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;hash(x);<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;hashVal&nbsp;</span><span style="COLOR: #000000">%=</span><span style="COLOR: #000000">theLists.size();<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;hashVal&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hashVal&nbsp;</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">&nbsp;theLists.size();<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;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;hashVal;<br></span><span style="COLOR: #008080">&nbsp;8</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">&nbsp;9</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span></div>
<p>&nbsp;&nbsp;&nbsp;&nbsp; 下面的例程中列出了一个可以存储在一般散列表中的Employee类，该类使用name成员作为键，Employee类通过提供相等性操作符和一个散列函数来实现HashedObj的需求。<br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: small; 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: #0000ff">class</span><span style="COLOR: #000000">&nbsp;Employee<br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img id=Codehighlighter1_15_224_Open_Image onclick="this.style.display='none'; Codehighlighter1_15_224_Open_Text.style.display='none'; Codehighlighter1_15_224_Closed_Image.style.display='inline'; Codehighlighter1_15_224_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_15_224_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_15_224_Closed_Text.style.display='none'; Codehighlighter1_15_224_Open_Image.style.display='inline'; Codehighlighter1_15_224_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_15_224_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_15_224_Open_Text><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/InBlock.gif" align=top></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;getname()&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img id=Codehighlighter1_68_83_Open_Image onclick="this.style.display='none'; Codehighlighter1_68_83_Open_Text.style.display='none'; Codehighlighter1_68_83_Closed_Image.style.display='inline'; Codehighlighter1_68_83_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_68_83_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_68_83_Closed_Text.style.display='none'; Codehighlighter1_68_83_Open_Image.style.display='inline'; Codehighlighter1_68_83_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_68_83_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_68_83_Open_Text><span style="COLOR: #000000">{&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;name;&nbsp;}</span></span><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>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;Employee&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">rhs&nbsp;)&nbsp;&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img id=Codehighlighter1_143_180_Open_Image onclick="this.style.display='none'; Codehighlighter1_143_180_Open_Text.style.display='none'; Codehighlighter1_143_180_Closed_Image.style.display='inline'; Codehighlighter1_143_180_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_143_180_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_143_180_Closed_Text.style.display='none'; Codehighlighter1_143_180_Open_Image.style.display='inline'; Codehighlighter1_143_180_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_143_180_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_143_180_Open_Text><span style="COLOR: #000000">{&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;getName()&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;rhs.getName();&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">operator</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;Employee&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;rhs&nbsp;}</span></span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img id=Codehighlighter1_240_266_Open_Image onclick="this.style.display='none'; Codehighlighter1_240_266_Open_Text.style.display='none'; Codehighlighter1_240_266_Closed_Image.style.display='inline'; Codehighlighter1_240_266_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_240_266_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_240_266_Closed_Text.style.display='none'; Codehighlighter1_240_266_Open_Image.style.display='inline'; Codehighlighter1_240_266_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_240_266_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_240_266_Open_Text><span style="COLOR: #000000">{&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">*</span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;rhs);&nbsp;}</span></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">private</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>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">&nbsp;name;<br></span><span style="COLOR: #008080">13</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">double</span><span style="COLOR: #000000">&nbsp;salary;<br></span><span style="COLOR: #008080">14</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">int</span><span style="COLOR: #000000">&nbsp;seniority;<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>};<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><br></span><span style="COLOR: #008080">17</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;hash(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;Employ&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">item&nbsp;)<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img id=Codehighlighter1_368_405_Open_Image onclick="this.style.display='none'; Codehighlighter1_368_405_Open_Text.style.display='none'; Codehighlighter1_368_405_Closed_Image.style.display='inline'; Codehighlighter1_368_405_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top></span><span id=Codehighlighter1_368_405_Open_Text><span style="COLOR: #000000">{<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;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;hash(&nbsp;item.getName()&nbsp;);<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div>
<p>&nbsp;&nbsp;&nbsp; 实现makeEmpty,contains和remove的程序代码如下<br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: small; 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: #0000ff">void</span><span style="COLOR: #000000">&nbsp;makeEmpty()<br></span><span style="COLOR: #008080">&nbsp;2 </span><span id=Codehighlighter1_17_90_Open_Text><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/InBlock.gif" align=top>&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</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">theLists.size();i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">&nbsp;)<br></span><span style="COLOR: #008080">&nbsp;4</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;theLists[i].clear();<br></span><span style="COLOR: #008080">&nbsp;5</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">&nbsp;6</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><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></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;contains(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;HashedObj&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">x&nbsp;)&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img id=Codehighlighter1_135_271_Open_Image onclick="this.style.display='none'; Codehighlighter1_135_271_Open_Text.style.display='none'; Codehighlighter1_135_271_Closed_Image.style.display='inline'; Codehighlighter1_135_271_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top></span><span id=Codehighlighter1_135_271_Open_Text><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/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;list</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">HashedObj</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;whichList&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;theLists[myhash(x)];<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;find(&nbsp;whichList.begin(),whichList.end(),x)&nbsp;</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">whichList.end();<br></span><span style="COLOR: #008080">11</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">12</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;remove(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;HashedObj&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">x&nbsp;)<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img id=Codehighlighter1_308_562_Open_Image onclick="this.style.display='none'; Codehighlighter1_308_562_Open_Text.style.display='none'; Codehighlighter1_308_562_Closed_Image.style.display='inline'; Codehighlighter1_308_562_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top></span><span id=Codehighlighter1_308_562_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;list</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">HashedObj</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">whichList&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;theLists[myhash(x)];<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;&nbsp;&nbsp;&nbsp;list</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">HashedObj</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::iterator&nbsp;itr&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;find(whichList.begin(),whichList.end(),x);<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;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;itr&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;whichList.end()&nbsp;)<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<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;whichList.erase(&nbsp;itr&nbsp;);<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;</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">currentSize;<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;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">true</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/ExpandedBlockEnd.gif" align=top>}</span></span></div>
<p>&nbsp;&nbsp;&nbsp;&nbsp;下一个操作是插入例程。如果要插入的项已经存在，那么什么都不做；否则将其放至表的前端。该元素可以放在表的任何地方；此处使用push_back是最方便的。whichList是一个引用变量<br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: small; 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: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;insert&nbsp;(&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;HashedObj&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">x&nbsp;)<br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img id=Codehighlighter1_35_297_Open_Image onclick="this.style.display='none'; Codehighlighter1_35_297_Open_Text.style.display='none'; Codehighlighter1_35_297_Closed_Image.style.display='inline'; Codehighlighter1_35_297_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top></span><span id=Codehighlighter1_35_297_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_35_297_Open_Text><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/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;list</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">HashedObj</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;whichList&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;theLists[&nbsp;myhash(x)&nbsp;];<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;find(&nbsp;whichList.begin(),whichList.end(),x&nbsp;)</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">whichList.end()&nbsp;)<br></span><span style="COLOR: #008080">&nbsp;5</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">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">false</span><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>&nbsp;&nbsp;&nbsp;&nbsp;whichList.push_back(x);<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;<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>&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">currentSize&nbsp;</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">&nbsp;theLists.size()&nbsp;)<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;&nbsp;&nbsp;&nbsp;rehash();<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;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">true</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/ExpandedBlockEnd.gif" align=top>}</span></span></div>
<p><br></span><br></font>&nbsp;</p>
<img src ="http://www.cppblog.com/xiaoluoluo/aggbug/101935.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xiaoluoluo/" target="_blank">小罗罗</a> 2009-11-25 22:22 <a href="http://www.cppblog.com/xiaoluoluo/archive/2009/11/25/101935.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>这本书</title><link>http://www.cppblog.com/xiaoluoluo/archive/2009/11/25/101919.html</link><dc:creator>小罗罗</dc:creator><author>小罗罗</author><pubDate>Wed, 25 Nov 2009 10:43:00 GMT</pubDate><guid>http://www.cppblog.com/xiaoluoluo/archive/2009/11/25/101919.html</guid><wfw:comment>http://www.cppblog.com/xiaoluoluo/comments/101919.html</wfw:comment><comments>http://www.cppblog.com/xiaoluoluo/archive/2009/11/25/101919.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xiaoluoluo/comments/commentRss/101919.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xiaoluoluo/services/trackbacks/101919.html</trackback:ping><description><![CDATA[<span style="COLOR: #999999; FONT-FAMILY: 楷体_GB2312">摘自阿郎的《这本书》</span><br>&nbsp;&nbsp;&nbsp; <br><span style="FONT-SIZE: 12pt; COLOR: #808080; FONT-FAMILY: 楷体_GB2312">1.&nbsp; 开始变得成熟是在最痛苦的时候才换到的<br><br>&nbsp;&nbsp;&nbsp; 想要追求梦想是在最空虚的时候才决定的<br>&nbsp; <br>&nbsp;&nbsp;&nbsp; 睡过最舒服的觉是在很累的时候才发现的<br>&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 得过最珍贵的东西是在失去的时候才知道的<br><br>2&nbsp;&nbsp;&nbsp;想念一个人的时候 距离成了一种温柔<br><br>&nbsp;&nbsp;&nbsp; 离开一个人的时候 距离成了一种借口<br><br>&nbsp;&nbsp;&nbsp; 害怕一个人的时候 心情成了一种寂寞<br><br>&nbsp;&nbsp;&nbsp; 想要一个人的时候 寂寞成了一种享受<br><br>3&nbsp;&nbsp; 这个时代 房子建得再大<br><br>&nbsp;&nbsp;&nbsp; 也装不下那些想流浪的心<br><br>&nbsp;&nbsp;&nbsp; 这个时代 房子盖得再小<br><br>&nbsp;&nbsp;&nbsp; 也容得下那些想安定的人<br></span>
<img src ="http://www.cppblog.com/xiaoluoluo/aggbug/101919.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xiaoluoluo/" target="_blank">小罗罗</a> 2009-11-25 18:43 <a href="http://www.cppblog.com/xiaoluoluo/archive/2009/11/25/101919.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>