fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <functional>
  4. #include <tuple>
  5. #include <utility>
  6. #include <unordered_map>
  7.  
  8.  
  9.  
  10. typedef std::tuple<std::string,int,char> xkey_t;
  11.  
  12. struct key_hash : public std::unary_function<xkey_t, std::size_t>
  13. {
  14. std::size_t operator()(const xkey_t& k) const
  15. {
  16. return std::get<0>(k)[0] ^ std::get<1>(k) ^ std::get<2>(k);
  17. }
  18. };
  19.  
  20. struct key_equal : public std::binary_function<xkey_t, xkey_t, bool>
  21. {
  22. bool operator()(const xkey_t& v0, const xkey_t& v1) const
  23. {
  24. return (
  25. std::get<0>(v0) == std::get<0>(v1) &&
  26. std::get<1>(v0) == std::get<1>(v1) &&
  27. std::get<2>(v0) == std::get<2>(v1)
  28. );
  29. }
  30. };
  31.  
  32. struct data
  33. {
  34. std::string x;
  35. };
  36.  
  37. typedef std::unordered_map<xkey_t,data,key_hash,key_equal> map_t;
  38.  
  39.  
  40. int main()
  41. {
  42. map_t m;
  43. data d;
  44. d.x = "test data";
  45.  
  46. m[std::make_tuple("abc",1,'X')] = d;
  47.  
  48. auto itr = m.find(std::make_tuple(std::string("abc"),1,'X'));
  49.  
  50. if (m.end() != itr)
  51. {
  52. std::cout << "x: " << itr->second.x;
  53. }
  54.  
  55. return 0;
  56. }
Success #stdin #stdout 0s 3020KB
stdin
Standard input is empty
stdout
x: test data