C++ STL中的哈希表 hash_map
来源:互联网 发布:淘宝鹏飞运动折扣店 编辑:程序博客网 时间:2024/05/17 08:16
需要hash_map的原因:map底层实现是红黑数,查找的效率是O(log n)。。但是hash_map的底层实现是哈希表,它的查找效率是O(1)
当数据很大的时候,比如100w的时候,map找的话要比较20次左右,但是hash_map却只需要比较一到两次。。
所以hash_map就需要了。
在定义hash_map容器的时候,不仅需要指定键和值的类型,还需要指定hash函数和相等函数
(一)hash_map 的hash函数
hash< int>到底是什么样子?看看源码:
struct hash<int> { size_t operator()(int __x) const { return __x; }};
原来是个函数对象。在SGI STL中,提供了以下hash函数:
struct hash<char*>struct hash<const char*>struct hash<char> struct hash<unsigned char> struct hash<signed char>struct hash<short>struct hash<unsigned short> struct hash<int> struct hash<unsigned int>struct hash<long> struct hash<unsigned long>
也就是说,如果你的key使用的是以上类型中的一种,你都可以使用缺省的hash函数。当然你自己也可以定义自己的hash函数。对于自定义变量,你只能如此,例如对于string,就必须自定义hash函数。例如:
struct str_hash{ size_t operator()(const string& str) const { unsigned long __h = 0; for (size_t i = 0 ; i < str.size() ; i ++) __h = 5*__h + str[i]; return size_t(__h); }};//如果你希望利用系统定义的字符串hash函数,你可以这样写:struct str_hash{ size_t operator()(const string& str) const { return __stl_hash_string(str.c_str()); }};
在声明自己的哈希函数时要注意以下几点:
- 使用struct,然后重载operator().
- 返回是size_t
- 参数是你要hash的key的类型。
- 函数是const类型的。
(二)hash_map 的比较函数
在map中的比较函数,需要提供less函数。如果没有提供,缺省的也是less< Key> 。在hash_map中,要比较桶内的数据和key是否相等,因此需要的是是否等于的函数:equal_to< Key>
如果你使用一个自定义的数据类型,如struct mystruct, 或者const char* 的字符串,如何使用比较函数?
使用比较函数,有两种方法.
第一种是:重载==操作符,利用equal_to;看看下面的例子:
struct mystruct{ int iID; int len; bool operator==(const mystruct & my) const{ return (iID==my.iID) && (len==my.len) ; }};
这样,就可以使用equal_to< mystruct>作为比较函数了。
另一种方法就是使用函数对象。自定义一个比较函数体:
struct compare_str{ bool operator()(const char* p1, const char*p2) const{ return strcmp(p1,p2)==0; }};
有了compare_str,就可以使用hash_map了。
typedef hash_map<const char*, string, hash<const char*>, compare_str> StrIntMap;StrIntMap namemap;namemap["岳不群"]="华山派掌门人,人称君子剑";namemap["张三丰"]="武当掌门人,太极拳创始人";namemap["东方不败"]="第一高手,葵花宝典";
(三)如何在hash_map中加入自己定义的类型?
#include <hash_map>#include <string>#include <iostream>using namespace std;//define the classclass ClassA{ public: ClassA(int a):c_a(a){} int getvalue()const { return c_a;} void setvalue(int a){c_a;} private: int c_a;};//1 define the hash functionstruct hash_A{ size_t operator()(const class ClassA & A)const{ // return hash<int>(classA.getvalue()); return A.getvalue(); }};
//2 define the equal functionstruct equal_A{ bool operator()(const class ClassA & a1, const class ClassA & a2)const{ return a1.getvalue() == a2.getvalue(); }};int main(){ hash_map<ClassA, string, hash_A, equal_A> hmap; ClassA a1(12); hmap[a1]="I am 12"; ClassA a2(198877); hmap[a2]="I am 198877"; cout<<hmap[a1]<<endl; cout<<hmap[a2]<<endl; return 0;}
- C++ STL中的哈希表 hash_map
- c++STL中的hash_map自定义类。
- STL中的hash_map
- STL中的hash_map使用
- 【C++】STL的hash_map
- STL中的map、unordered_map、hash_map
- hash_map快于STL中的map
- [STL] __gnu_cxx::hash_map使用中的一些问题
- 【编程语言】SGI STL中的hash_map浅析
- [STL] __gnu_cxx::hash_map使用中的一些问题
- 关于STL中的map和hash_map
- 详解STL中的map和hash_map区别
- 关于STL中的map和hash_map
- 关于STL中的map和hash_map
- STL hash_map
- STL---hash_map
- STL hash_map
- stl-hash_map
- 关于QTreeView和QStandardModel联合理解,外加显示委托的理解(学习笔记,如有错误还望指出)
- 程序员技术练级攻略
- 2.分别输出整数及小数部分
- 让JTextArea始终显示最后一行文字
- 运用JavaMail技术实现云打印
- C++ STL中的哈希表 hash_map
- Hibernate注解方法使用总结
- 你会写软件开发文档吗?
- 原来百度手环开源了
- 关于出现“使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式”错误的可能原因
- 【转自tonywearme】我为什么要为纯虚函数写出实现
- 一些近期需要攻克的事情
- 学习 BroadcastReceiver
- 散列表_HashTable.c