User defined hash in C++
来源:互联网 发布:云计算标准和应用大会 编辑:程序博客网 时间:2024/05/21 06:17
/*Custom hash functionsRef: https://en.wikipedia.org/wiki/Unordered_associative_containers_(C%2B%2B)#Custom_hash_functionshttp://en.cppreference.com/w/cpp/utility/hash*/#include <iostream>#include <string>#include <iomanip>#include <unordered_set>using namespace std;struct S { string first_name; string second_name;};bool operator==(const S& lhs, const S& rhs){ return lhs.first_name == rhs.first_name && lhs.second_name == rhs.second_name;}// custom hash can be a standalone function object:struct MyHash { size_t operator()(S const& s) const { size_t h1 = hash<string> { }(s.first_name); size_t h2 = hash<string> { }(s.second_name); return h1 ^ (h2 << 1); // Combine the hash values for h1 and h2 }};// custom specialization of std::hash can be injected in namespace stdnamespace std{ template<> struct hash<S> { typedef S argument_type; typedef size_t result_type; result_type operator()(argument_type const& S) const { result_type const h1(hash<string>{}(S.first_name)); result_type const h2(hash<string>{}(S.second_name)); return h1 ^ (h2 << 1); } };}int main(){ string str = "Meet the new boss..."; size_t str_hash = hash<string> { }(str); cout << "hash(" << quoted(str) << ") = " << str_hash << '\n'; S obj = { "Hubert", "Farnsworth" }; cout << "hash(" << quoted(obj.first_name) << "," << quoted(obj.second_name) << ") = " << MyHash { }(obj) << " (using MyHash)\n or " << hash<S>{}(obj) << " (using std::hash)\n"; // custom hash makes it possible to use custom types in unordered containers // The example will use the injected std::hash specialization, // to use MyHash instead, pass it as a second template argument unordered_set<S> names = { obj, {"Bender", "Rodriguze"}, {"Leela", "Turanga"} }; for (auto& s : names) cout << quoted(s.first_name) << ' ' << quoted(s.second_name) << '\n';}
阅读全文
0 0
- User defined hash in C++
- User-Defined Types in C
- Expanding User-Defined Runtime Attributes in Xcode with Objective-C
- Lua_第27章 User-Defined Types in C
- Using User Defined Types in COM & ATL
- Using User Defined Types in COM & ATL
- 20050923Working Diary-- user defined message in Openh323
- Deep Copy in C# (Cloning for a user defined class)
- How to create user defined properties in Maven
- how to extend C style IO to user defined types
- DB2 C language external scalar user-defined function
- LNK2005 int * c? already defined in *.obj
- 13.4 User-defined conversions
- User-defined conversions
- User Defined Window Messages
- [PHP] User Defined Extension
- UDF (User-defined Function)
- python User-defined Exceptions
- 自动判断访问设备
- web——固定定位
- 面试题综合
- oracle 中to_char 和to_date函数
- 机器学习笔记-集成学习之Bagging,Boosting,随机森林三者特性对比
- User defined hash in C++
- oracle用户管理
- JavaScript设计模式之-----工厂模式
- zepto.js源码解读(一):初窥全貌
- windows下hiredis使用
- 恶意代码分析实战 Lab 7-2 习题笔记
- IDL read table from a .txt file
- 安卓7.0关机流程(显示)
- 分布式与集群