#include <iostream>
#include <string>
#include <tuple>
#include <utility>
#include <unordered_map>
typedef std::tuple<std::string,int,char> key_t;
struct key_hash : public std::unary_function<key_t, std::size_t>
{
std::size_t operator()(const key_t& k) const
{
return std::get<0>(k)[0] ^ std::get<1>(k) ^ std::get<2>(k);
}
};
struct key_equal : public std::binary_function<key_t, key_t, bool>
{
bool operator()(const key_t& v0, const key_t& v1) const
{
return (
std::get<0>(v0) == std::get<0>(v1) &&
std::get<1>(v0) == std::get<1>(v1) &&
std::get<2>(v0) == std::get<2>(v1)
);
}
};
struct data
{
std::string x;
};
typedef std::unordered_map<const key_t,data,key_hash,key_equal> map_t;
int main()
{
map_t m;
data d;
d.x = "test data";
m[std::make_tuple("abc",1,'X')] = d;
auto itr = m.find(std::make_tuple(std::string("abc"),1,'X'));
if (m.end() != itr)
{
std::cout << "x: " << itr->second.x;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8dHVwbGU+CiNpbmNsdWRlIDx1dGlsaXR5PgojaW5jbHVkZSA8dW5vcmRlcmVkX21hcD4KCnR5cGVkZWYgc3RkOjp0dXBsZTxzdGQ6OnN0cmluZyxpbnQsY2hhcj4ga2V5X3Q7CgpzdHJ1Y3Qga2V5X2hhc2ggOiBwdWJsaWMgc3RkOjp1bmFyeV9mdW5jdGlvbjxrZXlfdCwgc3RkOjpzaXplX3Q+CnsKICAgc3RkOjpzaXplX3Qgb3BlcmF0b3IoKShjb25zdCBrZXlfdCYgaykgY29uc3QKICAgewogICAgICByZXR1cm4gc3RkOjpnZXQ8MD4oaylbMF0gXiBzdGQ6OmdldDwxPihrKSBeIHN0ZDo6Z2V0PDI+KGspOwogICB9Cn07CgpzdHJ1Y3Qga2V5X2VxdWFsIDogcHVibGljIHN0ZDo6YmluYXJ5X2Z1bmN0aW9uPGtleV90LCBrZXlfdCwgYm9vbD4KewogICBib29sIG9wZXJhdG9yKCkoY29uc3Qga2V5X3QmIHYwLCBjb25zdCBrZXlfdCYgdjEpIGNvbnN0CiAgIHsKICAgICAgcmV0dXJuICgKICAgICAgICAgICAgICAgc3RkOjpnZXQ8MD4odjApID09IHN0ZDo6Z2V0PDA+KHYxKSAmJgogICAgICAgICAgICAgICBzdGQ6OmdldDwxPih2MCkgPT0gc3RkOjpnZXQ8MT4odjEpICYmCiAgICAgICAgICAgICAgIHN0ZDo6Z2V0PDI+KHYwKSA9PSBzdGQ6OmdldDwyPih2MSkKICAgICAgICAgICAgICk7CiAgIH0KfTsKCnN0cnVjdCBkYXRhCnsKICAgc3RkOjpzdHJpbmcgeDsKfTsKCnR5cGVkZWYgc3RkOjp1bm9yZGVyZWRfbWFwPGNvbnN0IGtleV90LGRhdGEsa2V5X2hhc2gsa2V5X2VxdWFsPiBtYXBfdDsKCgppbnQgbWFpbigpCnsKICAgbWFwX3QgbTsKICAgZGF0YSBkOwogICBkLnggPSAidGVzdCBkYXRhIjsKICAgbVtzdGQ6Om1ha2VfdHVwbGUoImFiYyIsMSwnWCcpXSA9IGQ7CiAgIGF1dG8gaXRyID0gbS5maW5kKHN0ZDo6bWFrZV90dXBsZShzdGQ6OnN0cmluZygiYWJjIiksMSwnWCcpKTsKICAgaWYgKG0uZW5kKCkgIT0gaXRyKQogICB7CiAgICAgIHN0ZDo6Y291dCA8PCAieDogIiA8PCBpdHItPnNlY29uZC54OwogICB9CiAgIHJldHVybiAwOwp9
prog.cpp:7:42: error: conflicting declaration 'typedef class std::tuple<std::basic_string<char>, int, char> key_t'
/usr/include/sys/types.h:123:17: error: 'key_t' has a previous declaration as 'typedef __key_t key_t'
prog.cpp: In member function 'size_t key_hash::operator()(const key_t&) const':
prog.cpp:13:27: error: no matching function for call to 'get(const key_t&)'
prog.cpp:13:47: error: no matching function for call to 'get(const key_t&)'
prog.cpp:13:64: error: no matching function for call to 'get(const key_t&)'
prog.cpp: In member function 'bool key_equal::operator()(const key_t&, const key_t&) const':
prog.cpp:22:30: error: no matching function for call to 'get(const key_t&)'
prog.cpp:22:49: error: no matching function for call to 'get(const key_t&)'
prog.cpp:23:30: error: no matching function for call to 'get(const key_t&)'
prog.cpp:23:49: error: no matching function for call to 'get(const key_t&)'
prog.cpp:24:30: error: no matching function for call to 'get(const key_t&)'
prog.cpp:24:49: error: no matching function for call to 'get(const key_t&)'
prog.cpp: In function 'int main()':
prog.cpp:42:34: error: no match for 'operator[]' in 'm[std::make_tuple(_Elements&& ...) [with _Elements = {const char (&)[4], int, char}, typename std::__decay_and_strip<_Elements>::__type = <type error>](((int&&)(&1)), ((char&&)(&'X')))]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/hashtable_policy.h:531:5: note: candidate is: std::__detail::_Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::mapped_type& std::__detail::_Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::operator[](const _Key&) [with _Key = const int, _Pair = std::pair<const int, data>, _Hashtable = std::_Hashtable<const int, std::pair<const int, data>, std::allocator<std::pair<const int, data> >, std::_Select1st<std::pair<const int, data> >, key_equal, key_hash, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, false, false, true>, std::__detail::_Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::mapped_type = data]
prog.cpp:43:63: error: no matching function for call to 'std::unordered_map<const int, data, key_hash, key_equal>::find(std::tuple<std::basic_string<char>, int, char>)'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/hashtable.h:741:5: note: candidates are: std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::iterator std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::find(const key_type&) [with _Key = const int, _Value = std::pair<const int, data>, _Allocator = std::allocator<std::pair<const int, data> >, _ExtractKey = std::_Select1st<std::pair<const int, data> >, _Equal = key_equal, _H1 = key_hash, _H2 = std::__detail::_Mod_range_hashing, _Hash = std::__detail::_Default_ranged_hash, _RehashPolicy = std::__detail::_Prime_rehash_policy, bool __cache_hash_code = false, bool __constant_iterators = false, bool __unique_keys = true, std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::iterator = std::__detail::_Hashtable_iterator<std::pair<const int, data>, false, false>, key_type = const int]
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/hashtable.h:758:5: note: std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::const_iterator std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::find(const key_type&) const [with _Key = const int, _Value = std::pair<const int, data>, _Allocator = std::allocator<std::pair<const int, data> >, _ExtractKey = std::_Select1st<std::pair<const int, data> >, _Equal = key_equal, _H1 = key_hash, _H2 = std::__detail::_Mod_range_hashing, _Hash = std::__detail::_Default_ranged_hash, _RehashPolicy = std::__detail::_Prime_rehash_policy, bool __cache_hash_code = false, bool __constant_iterators = false, bool __unique_keys = true, std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::const_iterator = std::__detail::_Hashtable_const_iterator<std::pair<const int, data>, false, false>, key_type = const int]
prog.cpp:43:63: error: unable to deduce 'auto' from '<expression error>'