C++中的哈希容器unordered_map使用示例

C++中的哈希容器unordered_map使用示例

投稿:junjie 字体:[增加 减小] 类型:转载 时间:2015-06-10 我要评论

这篇文章主要介绍了C++中的哈希容器unordered_map使用示例,本文直接给出实例代码,并讲解了一些hash table的知识,需要的朋友可以参考下
id="iframeu2261530_0" src="http://pos.baidu.com/dcgm?sz=680x200&rdid=2261530&dc=2&di=u2261530&dri=0&dis=0&dai=2&ps=0x0&coa=at%3D3%26rsi0%3D680%26rsi1%3D200%26pat%3D6%26tn%3DbaiduCustNativeAD%26rss1%3D%2523FFFFFF%26conBW%3D1%26adp%3D1%26ptt%3D0%26titFF%3D%2525E5%2525BE%2525AE%2525E8%2525BD%2525AF%2525E9%25259B%252585%2525E9%2525BB%252591%26titFS%3D14%26rss2%3D%2523000000%26titSU%3D0%26ptbg%3D90%26piw%3D0%26pih%3D0%26ptp%3D0&dcb=BAIDU_SSP_define&dtm=HTML_POST&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1477810030322&ti=C%2B%2B%E4%B8%AD%E7%9A%84%E5%93%88%E5%B8%8C%E5%AE%B9%E5%99%A8unordered_map%E4%BD%BF%E7%94%A8%E7%A4%BA%E4%BE%8B_C%20%E8%AF%AD%E8%A8%80_%E8%84%9A%E6%9C%AC%E4%B9%8B%E5%AE%B6&ari=2&dbv=2&drs=1&pcs=1347x638&pss=1347x2153&cfv=0&cpl=5&chi=1&cce=true&cec=GBK&tlm=1477321834&rw=638&ltu=http%3A%2F%2Fwww.jb51.net%2Farticle%2F67583.htm&ltr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DBNQK-HAZ3krjO7took4hOuOiiFviYTyHwXkzxfUUGCUUgkb5Sushyk8hAvzbrRyC%26wd%3D%26eqid%3Dec30124a000078ea0000000258159766&ecd=1&psr=1366x768&par=1366x728&pis=-1x-1&ccd=24&cja=false&cmi=7&col=zh-CN&cdo=-1&tcn=1477810030&qn=bb7a6605c81e1784&tt=1477810029970.488.589.591" width="680" height="200" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="display: block; border-width: 0px; border-style: initial; vertical-align: bottom; margin: 0px;">

随着C++0x标准的确立,C++的标准库中也终于有了hash table这个东西。

很久以来,STL中都只提供<map>作为存放对应关系的容器,内部通常用红黑树实现,据说原因是二叉平衡树(如红黑树)的各种操作,插入、删除、查找等,都是稳定的时间复杂度,即O(log n);但是对于hash表来说,由于无法避免re-hash所带来的性能问题,即使大多数情况下hash表的性能非常好,但是re-hash所带来的不稳定性在当时是不能容忍的。

不过由于hash表的性能优势,它的使用面还是很广的,于是第三方的类库基本都提供了支持,比如MSVC中的<hash_map>和Boost中的<boost/unordered_map.hpp>。后来Boost的unordered_map被吸纳进了TR1 (C++ Technical Report 1),然后在C++0x中被最终定了标准。

于是我们现在就可以开心得写以下的代码了:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include <unordered_map>
  
int main()
{
  std::unordered_map<std::string, int > months;
  months[ "january" ] = 31;
  months[ "february" ] = 28;
  months[ "march" ] = 31;
  months[ "april" ] = 30;
  months[ "may" ] = 31;
  months[ "june" ] = 30;
  months[ "july" ] = 31;
  months[ "august" ] = 31;
  months[ "september" ] = 30;
  months[ "october" ] = 31;
  months[ "november" ] = 30;
  months[ "december" ] = 31;
  std::cout << "september -> " << months[ "september" ] << std::endl;
  std::cout << "april   -> " << months[ "april" ] << std::endl;
  std::cout << "december -> " << months[ "december" ] << std::endl;
  std::cout << "february -> " << months[ "february" ] << std::endl;
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值