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