[c++11]hash<T>哈希结构模板
2017-02-18 23:22
274 查看
一、哈希结构模板hash简介
c++11新增的哈希结构模板定义于头文件<functional>:
template<class _Kty> struct _Bitwise_hash : public unary_function<_Kty, size_t> {...};
哈希结构模板定义一个函数对象(重载了operator()),实现了散列函数:
1.接受一个参数的类型Key
2.返回一个类型为size_t的值,表示该参数的哈希值
3.调用时不会抛出异常
4.若两个参数k1k2相等,则
hash<Key>()(k1) == hash<Key>()(k2)
5.若两个不同的参数k1k2不相等,则
hash<Key>()(k1) == hash<Key>()(k2)成立的概率应非常小,接近
1.0/std::numeric_limits<size_t>::max()
无序关联容器
unordered_set,unordered_multiset,unordered_map和
unordered_multimap默认使用哈希结构模板来为键计算散列值。
二、成员
成员类型
argument_type | 模板第一个类型参数 |
---|---|
result_type | size_t |
公有成员方法
默认构造函数 | 构造一个哈希函数对象 |
---|---|
size_t operator()(T &t) | 计算t的散列值 |
三、扩展和应用
在头文件里,实例化了内置类型的哈希结构模板:template<> struct hash<bool>; template<> struct hash<char>; template<> struct hash<signed char>; template<> struct hash<unsigned char>; template<> struct hash<char16_t>; template<> struct hash<char32_t>; template<> struct hash<wchar_t>; template<> struct hash<short>; template<> struct hash<unsigned short>; template<> struct hash<int>; template<> struct hash<unsigned int>; template<> struct hash<long>; template<> struct hash<long long>; template<> struct hash<unsigned long>; template<> struct hash<unsigned long long>; template<> struct hash<float>; template<> struct hash<double>; template<> struct hash<long double>; template< class T > struct hash<T*>;
C++11实例化了字符串的哈希结构模板
std::hash<std::string> std::hash<std::u16string> std::hash<std::u32string> std::hash<std::wstring>
C++11,std::error_code的哈希支持
std::hash<std::error_code>
C++11实例化的其他哈希结构模板
std::hash<std::bitset> std::hash<std::unique_ptr> std::hash<std::shared_ptr> std::hash<std::type_index> std::hash<std::vector<bool>> std::hash<std::thread::id>
四、代码示例
IDE:vs2013#include <iostream> #include <functional>// #include <string> #include <stdlib.h> using std::hash;// using std::string; using std::cout; //自定义类型 class S { public: string first_name; string last_name; }; //自己封装一个哈希函数对象的类型,内部使用了hash结构模板 class MyHash { public: size_t operator()(const S &s) const { size_t h1 = hash<string>()(s.first_name); size_t h2 = hash<string>()(s.last_name); return h1 ^ (h2 << 1); } }; //也可以用自定义类实例化一个hash结构模板 template<> class hash < S > { public: size_t operator()(const S &s) const { size_t h1 = hash<string>()(s.first_name); size_t h2 = hash<string>()(s.last_name); return h1 ^ (h2 << 1); } }; int main() { cout << "计算string的散列值的示例:\n"; string str = "Meet the new boss..."; hash<string> hash_fn; size_t str_hash = hash_fn(str); cout << str_hash << '\n'; cout << "\n计算自定义类型S的散列值的示例:\n"; string s1 = "Hubert"; string s2 = "Farnsworth"; hash<string> h1; S obj_S; obj_S.first_name = s1; obj_S.last_name = s2; cout << "hash(s1) = \t" << h1(s1) << "\n" << "hash(s2) = \t" << hash<string>()(s2) << "\n" << "MyHash(obj_S) = " << MyHash()(obj_S) << "\n" << "hash(obj_S) = \t" << hash<S>()(obj_S) << "\n"; ::system("pause"); return 0; }
输出:
如有错误,请各位看官不吝指正,: )
参考:http://zh.cppreference.com/w/cpp/utility/hash
相关文章推荐
- C++11 元编程 判断是否有std::hash<T>特例并提供hash函数通用实现
- .net集合类的研究--哈希表(二)--HashSet<T>
- c++11 <1>单生产者单消费者模式
- http://blog.csdn.net/sparkliang/article/details/5279393 <p> 一致性 hash 算法( consistent hashing )
- STL map<string,string> 未能从“const std::string”为“const std::_Revranit<_RanIt,_Base> &”推导 模板 参数
- android的ant脚本<build.xml>自动生成模板
- 如何实现具有层次结构的 TreeView <一> (WPF/TreeView/Style/Template)
- <模板> 矩形分割
- ASP.NET 4.0: 新的的模板语法代码块 <%: %>
- 如何实现具有层次结构的 TreeView <四> (WPF/TreeView/Style/Template)
- Redis-数据结构(哈希hash)
- STL hash<vector<bool>>
- 数据结构与算法(C语言)<绪论>
- IM 开源项目 登录服务器 基础表结构<2>
- ethernet,ip,tcp结构<备份记忆>
- C++11 并发指南四(<future> 详解三 std::future & std::shared_future)
- 重载 struct hash<class T>
- IEnumerable<IEnumerable<string>>结构解析通用解决方案(支持指定属性顺序)
- thinkphp 模板中<ol></ol>自定义序号
- 条件选择结构<2>__笔记(4)