fork(1) download
  1. #include <tuple>
  2. #include <unordered_map>
  3. #include <cassert>
  4. #include <iostream>
  5.  
  6. struct triplet
  7. {
  8. unsigned a,b,c;
  9. bool operator< (triplet const& o) const { return std::tie(a,b,c) < std::tie(o.a,o.b,o.c); }
  10. bool operator==(triplet const& o) const { return std::tie(a,b,c) ==std::tie(o.a,o.b,o.c); }
  11. };
  12.  
  13. namespace std {
  14. template<> struct hash<triplet> {
  15. unsigned int operator()(triplet const& key) const {
  16. return ~key.a + 17u*key.b + 17u*key.c; // totally made that up, could be better, I suppose
  17. }
  18. };
  19. }
  20.  
  21. static std::ostream& operator<<(std::ostream& os, triplet const& key) {
  22. return os << '[' << key.a << ',' << key.b << ',' << key.c << ']';
  23. }
  24.  
  25. int main()
  26. {
  27. std::unordered_multimap<triplet, double> map;
  28.  
  29. // insert items dynamically
  30. map.insert({ triplet{ /*I*/ 1, /*J*/ 2, /*K*/ 3 }, 0.1 } );
  31. map.insert({ triplet{ /*I*/ 4, /*J*/ 5, /*K*/ 6 }, 0.2 } );
  32. map.insert({ triplet{ /*I*/ 7, /*J*/ 8, /*K*/ 9 }, 0.3 } );
  33. map.insert({ triplet{ /*I*/ 1, /*J*/ 2, /*K*/ 0 }, 0.4 } ); // duplicate (I,J) ok
  34.  
  35. map.insert({ triplet{ /*I*/ 1, /*J*/ 2, /*K*/ 0 }, 0.5 } );
  36.  
  37. assert(0 == map.count(triplet {1,5,6}));
  38. assert(1 == map.count(triplet {4,5,6}));
  39.  
  40. auto range = map.equal_range(triplet { 1,2,0 });
  41. for (auto it=range.first; it!=range.second; ++it)
  42. std::cout << it->first << ": " << it->second << "\n";
  43. }
  44.  
Success #stdin #stdout 0s 3020KB
stdin
Standard input is empty
stdout
[1,2,0]: 0.4
[1,2,0]: 0.5