﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-公子周-随笔分类-字符串处理</title><link>http://www.cppblog.com/zxl200406/category/19594.html</link><description>&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp--不乱于心，不困于情</description><language>zh-cn</language><lastBuildDate>Sat, 07 Jul 2012 13:15:44 GMT</lastBuildDate><pubDate>Sat, 07 Jul 2012 13:15:44 GMT</pubDate><ttl>60</ttl><item><title>高效的hash_map</title><link>http://www.cppblog.com/zxl200406/archive/2012/07/07/182108.html</link><dc:creator>公子周</dc:creator><author>公子周</author><pubDate>Sat, 07 Jul 2012 12:45:00 GMT</pubDate><guid>http://www.cppblog.com/zxl200406/archive/2012/07/07/182108.html</guid><wfw:comment>http://www.cppblog.com/zxl200406/comments/182108.html</wfw:comment><comments>http://www.cppblog.com/zxl200406/archive/2012/07/07/182108.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zxl200406/comments/commentRss/182108.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zxl200406/services/trackbacks/182108.html</trackback:ping><description><![CDATA[<div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; ">#include &lt;iostream&gt;</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; ">#include &lt;hash_map&gt;</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; ">#include &lt;ctime&gt;</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; ">using namespace std;</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><br /></span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; ">int main()</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; ">{</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; ">	</span>hash_map&lt;int,int&gt; hm;</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; ">	</span>clock_t startx = clock();</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; ">	</span>for(int i = 0; i &lt; 10000000; i++)</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; ">		</span>hm.insert(make_pair(i,i));</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><br /></span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; "></span><div>&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; cout&lt;&lt;"插入"&lt;&lt;(double)(clock()-startx)/CLOCKS_PER_SEC&lt;&lt;endl;</div></span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><br /></span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><br /></span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; ">	</span>clock_t start = clock();</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; ">	</span>clock_t finish;</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; ">	</span>double totaltime;</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><br /></span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; ">	</span>for(int i = 0; i &lt; 10000000; i++)</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; ">	</span>{</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; ">		</span>if(hm.find(i)== hm.end())</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; ">			</span>cout&lt;&lt;"Error!";</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; ">		</span>if(i%1000000 == 0)</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; ">		</span>{</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; ">			</span>finish = clock();</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; ">			</span>totaltime = (double)(finish-start)/CLOCKS_PER_SEC;</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; ">			</span>cout&lt;&lt;i&lt;&lt;" "&lt;&lt; totaltime &lt;&lt; endl;</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; ">		</span>}</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; ">	</span>}</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><br /></span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; ">	</span>finish = clock();</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; ">	</span>totaltime=(double)(finish-start)/CLOCKS_PER_SEC;</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; ">	</span>cout&lt;&lt;"\nRuntime is: "&lt;&lt; totaltime &lt;&lt; "s" &lt;&lt; endl;</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; ">	</span>//system("pause");</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; "><span style="white-space: pre; ">	</span>return 0;</span></div><div style="padding: 0px; color: #000000; font-family: verdana, 'courier new'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span style="font-size: 12px; ">}</span><br /><br /><span style="color: red;">Release版本</span>，debug太慢了<br /><div style="padding: 0px; "><span style="font-size: 12px; ">插入3.715</span></div><div style="padding: 0px; "><span style="font-size: 12px; ">0 0</span></div><div style="padding: 0px; "><span style="font-size: 12px; ">1000000 0.133</span></div><div style="padding: 0px; "><span style="font-size: 12px; ">2000000 0.266</span></div><div style="padding: 0px; "><span style="font-size: 12px; ">3000000 0.399</span></div><div style="padding: 0px; "><span style="font-size: 12px; ">4000000 0.534</span></div><div style="padding: 0px; "><span style="font-size: 12px; ">5000000 0.683</span></div><div style="padding: 0px; "><span style="font-size: 12px; ">6000000 0.833</span></div><div style="padding: 0px; "><span style="font-size: 12px; ">7000000 0.999</span></div><div style="padding: 0px; "><span style="font-size: 12px; ">8000000 1.149</span></div><div style="padding: 0px; "><span style="font-size: 12px; ">9000000 1.295</span></div><div style="padding: 0px; "><span style="font-size: 12px; "><br /></span></div><div style="padding: 0px; "><span style="font-size: 12px; ">Runtime is: 1.441s</span></div></div></div><img src ="http://www.cppblog.com/zxl200406/aggbug/182108.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zxl200406/" target="_blank">公子周</a> 2012-07-07 20:45 <a href="http://www.cppblog.com/zxl200406/archive/2012/07/07/182108.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>