﻿<?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++博客-时雨の记-RainCode</title><link>http://www.cppblog.com/lams/</link><description>远野嘉一(Lams Lupin)的专栏</description><language>zh-cn</language><lastBuildDate>Sun, 19 Apr 2026 11:01:34 GMT</lastBuildDate><pubDate>Sun, 19 Apr 2026 11:01:34 GMT</pubDate><ttl>60</ttl><item><title>浅谈哈希思想的应用</title><link>http://www.cppblog.com/lams/archive/2011/09/10/hashtable.html</link><dc:creator>远野嘉一</dc:creator><author>远野嘉一</author><pubDate>Sat, 10 Sep 2011 04:07:00 GMT</pubDate><guid>http://www.cppblog.com/lams/archive/2011/09/10/hashtable.html</guid><wfw:comment>http://www.cppblog.com/lams/comments/155502.html</wfw:comment><comments>http://www.cppblog.com/lams/archive/2011/09/10/hashtable.html#Feedback</comments><slash:comments>8</slash:comments><wfw:commentRss>http://www.cppblog.com/lams/comments/commentRss/155502.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lams/services/trackbacks/155502.html</trackback:ping><description><![CDATA[<dl>
<dt><span style="font-size: 14pt"><strong>前言</strong></span></dt>
<dt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;散列表（HashTable）又称为哈希表，是一种快速的数据查找结构，它通常是为一个（组）要记录的数据设计一个哈希函数H(x)，依据这个函数进行给数据定位，如果是闭散列，那就是直接存到数组的H(x)下标处，如果是开散列，就是存到指针数组H(x)下标的链表处。在OI中某些Pascaler为了避开链表而采用的闭散列鄙人认为相当糟糕，至于原因会在后面解释。所以本文只谈开散列。<br /><br /><strong style="font-size: 14pt">哈希表的组织方式：</strong><strong><br /></strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;我们首先要确定一个哈希函数H(x)，x是要记录的对象，我们以H(x)来确定对象的记录的链的位置。<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;还需要一个指针数组来存放每个链的头指针。由于要使用链表，所以还要有一个class/struct作为链表的基本单位。<br /></dt>
<dt><strong style="font-size: 14pt">哈希表的一般实现：</strong><strong><br /></strong>首先是链表的基本元素：</dt></dl>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">template</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;T</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;t_node<br /><img id="Codehighlighter1_32_104_Open_Image" onclick="this.style.display='none'; Codehighlighter1_32_104_Open_Text.style.display='none'; Codehighlighter1_32_104_Closed_Image.style.display='inline'; Codehighlighter1_32_104_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_32_104_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_32_104_Closed_Text.style.display='none'; Codehighlighter1_32_104_Open_Image.style.display='inline'; Codehighlighter1_32_104_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_32_104_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_32_104_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">public</span><span style="color: #000000">:<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;key;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">other&nbsp;info</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t_node</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;next;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000">;</span></div>
<p>然后是HashTable类的骨架（我在这里把它封装成类了）：</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">template</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;T</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;hashtable<br /><img id="Codehighlighter1_34_294_Open_Image" onclick="this.style.display='none'; Codehighlighter1_34_294_Open_Text.style.display='none'; Codehighlighter1_34_294_Closed_Image.style.display='inline'; Codehighlighter1_34_294_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_34_294_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_34_294_Closed_Text.style.display='none'; Codehighlighter1_34_294_Open_Image.style.display='inline'; Codehighlighter1_34_294_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_34_294_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_34_294_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">public</span><span style="color: #000000">:<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hashtable();<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;hash(</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;T&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">sr);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;insert();<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t_node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">find(</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;T&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">sr);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">add&nbsp;more&nbsp;functions</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">private</span><span style="color: #000000">:<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t_node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">ht[t_size];</span><span style="color: #008000">//</span><span style="color: #008000">you&nbsp;should&nbsp;define&nbsp;t_size&nbsp;as&nbsp;sth&nbsp;before<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">add&nbsp;more&nbsp;things</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" /></span><span style="color: #000000">}</span></span><span style="color: #000000">;</span></div>
<p>接下来是构造函数：</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">hashtable</span><span style="color: #000000">&lt;</span><span style="color: #000000">T</span><span style="color: #000000">&gt;</span><span style="color: #000000">::hahstable()<br /><img id="Codehighlighter1_26_57_Open_Image" onclick="this.style.display='none'; Codehighlighter1_26_57_Open_Text.style.display='none'; Codehighlighter1_26_57_Closed_Image.style.display='inline'; Codehighlighter1_26_57_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_26_57_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_26_57_Closed_Text.style.display='none'; Codehighlighter1_26_57_Open_Image.style.display='inline'; Codehighlighter1_26_57_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_26_57_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_26_57_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;memset(ht,</span><span style="color: #000000">0</span><span style="color: #000000">,</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(ht));<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span></div>
<p>先略去哈希函数，介绍插入函数：</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;hashtable</span><span style="color: #000000">&lt;</span><span style="color: #000000">T</span><span style="color: #000000">&gt;</span><span style="color: #000000">::insert(</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;T&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">sr)<br /><img id="Codehighlighter1_39_619_Open_Image" onclick="this.style.display='none'; Codehighlighter1_39_619_Open_Text.style.display='none'; Codehighlighter1_39_619_Closed_Image.style.display='inline'; Codehighlighter1_39_619_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_39_619_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_39_619_Closed_Text.style.display='none'; Codehighlighter1_39_619_Open_Image.style.display='inline'; Codehighlighter1_39_619_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_39_619_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_39_619_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;loc&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;hash(sr);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(ht[loc]&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">)<br /><img id="Codehighlighter1_91_179_Open_Image" onclick="this.style.display='none'; Codehighlighter1_91_179_Open_Text.style.display='none'; Codehighlighter1_91_179_Closed_Image.style.display='inline'; Codehighlighter1_91_179_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_91_179_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_91_179_Closed_Text.style.display='none'; Codehighlighter1_91_179_Open_Image.style.display='inline'; Codehighlighter1_91_179_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_91_179_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_91_179_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">此处为空，插入一个新链表</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ht[loc]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;t_node();<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ht[loc]</span><span style="color: #000000">-&gt;</span><span style="color: #000000">&nbsp;key&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;T;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000"><br /><img id="Codehighlighter1_194_617_Open_Image" onclick="this.style.display='none'; Codehighlighter1_194_617_Open_Text.style.display='none'; Codehighlighter1_194_617_Closed_Image.style.display='inline'; Codehighlighter1_194_617_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_194_617_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_194_617_Closed_Text.style.display='none'; Codehighlighter1_194_617_Open_Image.style.display='inline'; Codehighlighter1_194_617_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_194_617_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_194_617_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t_node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">now&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;ht[loc];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">true</span><span style="color: #000000">)<br /><img id="Codehighlighter1_256_611_Open_Image" onclick="this.style.display='none'; Codehighlighter1_256_611_Open_Text.style.display='none'; Codehighlighter1_256_611_Closed_Image.style.display='inline'; Codehighlighter1_256_611_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_256_611_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_256_611_Closed_Text.style.display='none'; Codehighlighter1_256_611_Open_Image.style.display='inline'; Codehighlighter1_256_611_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_256_611_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_256_611_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(now</span><span style="color: #000000">-&gt;</span><span style="color: #000000">key&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;sr)<br /><img id="Codehighlighter1_302_367_Open_Image" onclick="this.style.display='none'; Codehighlighter1_302_367_Open_Text.style.display='none'; Codehighlighter1_302_367_Closed_Image.style.display='inline'; Codehighlighter1_302_367_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_302_367_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_302_367_Closed_Text.style.display='none'; Codehighlighter1_302_367_Open_Image.style.display='inline'; Codehighlighter1_302_367_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_302_367_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_302_367_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">元素已经存在。&nbsp;</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(now</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">)<br /><img id="Codehighlighter1_418_567_Open_Image" onclick="this.style.display='none'; Codehighlighter1_418_567_Open_Text.style.display='none'; Codehighlighter1_418_567_Closed_Image.style.display='inline'; Codehighlighter1_418_567_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_418_567_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_418_567_Closed_Text.style.display='none'; Codehighlighter1_418_567_Open_Image.style.display='inline'; Codehighlighter1_418_567_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_418_567_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_418_567_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">链里面没有该元素，就地插入</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;now</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;t_node();<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;now</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next</span><span style="color: #000000">-&gt;</span><span style="color: #000000">key&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;T;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;now&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;now</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span></div>
<p>然后是查找：</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">t_node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">hashtable</span><span style="color: #000000">&lt;</span><span style="color: #000000">T</span><span style="color: #000000">&gt;</span><span style="color: #000000">::find(</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;T&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">st)<br /><img id="Codehighlighter1_40_519_Open_Image" onclick="this.style.display='none'; Codehighlighter1_40_519_Open_Text.style.display='none'; Codehighlighter1_40_519_Closed_Image.style.display='inline'; Codehighlighter1_40_519_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_40_519_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_40_519_Closed_Text.style.display='none'; Codehighlighter1_40_519_Open_Image.style.display='inline'; Codehighlighter1_40_519_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_40_519_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_40_519_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;loc&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;hash(sr);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(ht[loc]&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">)<br /><img id="Codehighlighter1_92_142_Open_Image" onclick="this.style.display='none'; Codehighlighter1_92_142_Open_Text.style.display='none'; Codehighlighter1_92_142_Closed_Image.style.display='inline'; Codehighlighter1_92_142_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_92_142_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_92_142_Closed_Text.style.display='none'; Codehighlighter1_92_142_Open_Image.style.display='inline'; Codehighlighter1_92_142_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_92_142_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_92_142_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">此处为空，木有~&nbsp;返回空指针&nbsp;</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000"><br /><img id="Codehighlighter1_157_517_Open_Image" onclick="this.style.display='none'; Codehighlighter1_157_517_Open_Text.style.display='none'; Codehighlighter1_157_517_Closed_Image.style.display='inline'; Codehighlighter1_157_517_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_157_517_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_157_517_Closed_Text.style.display='none'; Codehighlighter1_157_517_Open_Image.style.display='inline'; Codehighlighter1_157_517_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_157_517_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_157_517_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t_node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">now&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;ht[loc];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">true</span><span style="color: #000000">)<br /><img id="Codehighlighter1_219_511_Open_Image" onclick="this.style.display='none'; Codehighlighter1_219_511_Open_Text.style.display='none'; Codehighlighter1_219_511_Closed_Image.style.display='inline'; Codehighlighter1_219_511_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_219_511_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_219_511_Closed_Text.style.display='none'; Codehighlighter1_219_511_Open_Image.style.display='inline'; Codehighlighter1_219_511_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_219_511_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_219_511_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(now</span><span style="color: #000000">-&gt;</span><span style="color: #000000">key&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;sr)<br /><img id="Codehighlighter1_265_330_Open_Image" onclick="this.style.display='none'; Codehighlighter1_265_330_Open_Text.style.display='none'; Codehighlighter1_265_330_Closed_Image.style.display='inline'; Codehighlighter1_265_330_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_265_330_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_265_330_Closed_Text.style.display='none'; Codehighlighter1_265_330_Open_Image.style.display='inline'; Codehighlighter1_265_330_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_265_330_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_265_330_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">找到了&nbsp;</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;now;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(now</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">)<br /><img id="Codehighlighter1_381_454_Open_Image" onclick="this.style.display='none'; Codehighlighter1_381_454_Open_Text.style.display='none'; Codehighlighter1_381_454_Closed_Image.style.display='inline'; Codehighlighter1_381_454_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_381_454_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_381_454_Closed_Text.style.display='none'; Codehighlighter1_381_454_Open_Image.style.display='inline'; Codehighlighter1_381_454_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_381_454_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_381_454_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">遍历完了整个链还是木有。。&nbsp;</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;now&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;now</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next;</span><span style="color: #008000">//</span><span style="color: #008000">看这个链的下一个元素&nbsp;</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span></div>
<p>当然可以根据具体情况做各种改动，如果要极限追求效率可以在t_node里面把key改为指针，然后使用自己编写的内存分配函数代替new。<br /><br /><br /><span style="font-size: 14pt"><strong>最简单的哈希函数：</strong></span><br />其实最简单的哈希表1就是H(x)=x，意思是若记录对象是整数，就直接采用这个整数为下标（char类型也可视为整数），这个就是数组，但它也可以看作哈希表。<br />最简单的哈希表2就是H(x)=1，意思是不管是什么元素都放到同一个下标，这个就是链表，也可视为一种哈希表。<br /><br /><span style="font-size: 14pt"><strong>大整数的哈希函数：</strong></span><br />当记录对象是大整数的时候，若再用H(x)=x，数组的范围将会承受不起，所以这时候要考虑哈希函数的设计问题，又有很多种设计方法，最广泛的一种就是H(x)=x%k，k通常是一个质数。<br /><br /><strong style="font-size: 14pt">一般的哈希函数：</strong><strong><br /></strong>我们也许会记录一些class或者struct之类的东西，这时候我们可以选取里面的某些关键变量进行一种运算来确定下标。<br /><br /><strong style="font-size: 14pt">冲突的处理：</strong><strong><br /></strong>再好的哈希函数也很难避免冲突，所谓冲突就是说H(a)=H(b)的情况，而开散列的处理方法是在数组后面挂的是链表，这样冲突的元素可以直接挂在链表的末端，而闭散列没有链表，一般是重复Hn(x)或者往H(x)+a(a=1,2,3..)寻找，这会使哈希表变得一塌糊涂，而且冲突还可能引发别的冲突，而且也不便于估计哈希数组的范围，所以鄙人不提倡使用闭散列的组织方式。<br />顺便说一句：好的哈希函数是尽量减少和平衡冲突，尽量使得每个链的长度分布得平均，好的哈希函数的设计要靠长久的经验积累，绝非一日之功。<br /><br /><span style="font-size: 14pt"><strong>哈希表的本质思想：</strong></span><br />散列表本质思想就是把数组与链表的优势结合起来，数组的访问复杂度是O(1)，链表的插入复杂度是O(1)，然而数组的插入复杂度和链表的访问复杂度都比较高，所以就产生了散列表。我们可以把这个思想运用到许多地方，这本是我想说的重点，但鄙人才疏学浅，不知如何表达，日后整理一下代码说明吧。</p><img src ="http://www.cppblog.com/lams/aggbug/155502.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lams/" target="_blank">远野嘉一</a> 2011-09-10 12:07 <a href="http://www.cppblog.com/lams/archive/2011/09/10/hashtable.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hello world</title><link>http://www.cppblog.com/lams/archive/2011/09/09/155449.html</link><dc:creator>远野嘉一</dc:creator><author>远野嘉一</author><pubDate>Fri, 09 Sep 2011 06:23:00 GMT</pubDate><guid>http://www.cppblog.com/lams/archive/2011/09/09/155449.html</guid><wfw:comment>http://www.cppblog.com/lams/comments/155449.html</wfw:comment><comments>http://www.cppblog.com/lams/archive/2011/09/09/155449.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lams/comments/commentRss/155449.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lams/services/trackbacks/155449.html</trackback:ping><description><![CDATA[<div>今天刚开了CppBlog，以后会常来写我的Coding感受和经验，欢迎广大同行批评指点，不尽感激。<br />
<h3><a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#108;&#97;&#109;&#115;&#64;&#118;&#105;&#112;&#46;&#113;&#113;&#46;&#99;&#111;&#109;">lams@vip.qq.com</a></h3></div><img src ="http://www.cppblog.com/lams/aggbug/155449.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lams/" target="_blank">远野嘉一</a> 2011-09-09 14:23 <a href="http://www.cppblog.com/lams/archive/2011/09/09/155449.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>