You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.0 KiB

2 months ago
  1. #include <iostream>
  2. #include <string>
  3. #include <sparsepp/spp.h>
  4. using std::string;
  5. struct Person
  6. {
  7. bool operator==(const Person &o) const
  8. {
  9. return _first == o._first && _last == o._last;
  10. }
  11. string _first;
  12. string _last;
  13. };
  14. namespace std
  15. {
  16. // inject specialization of std::hash for Person into namespace std
  17. // ----------------------------------------------------------------
  18. template<>
  19. struct hash<Person>
  20. {
  21. std::size_t operator()(Person const &p) const
  22. {
  23. std::size_t seed = 0;
  24. spp::hash_combine(seed, p._first);
  25. spp::hash_combine(seed, p._last);
  26. return seed;
  27. }
  28. };
  29. }
  30. int main()
  31. {
  32. // As we have defined a specialization of std::hash() for Person,
  33. // we can now create sparse_hash_set or sparse_hash_map of Persons
  34. // ----------------------------------------------------------------
  35. spp::sparse_hash_set<Person> persons =
  36. { { "John", "Galt" },
  37. { "Jane", "Doe" }
  38. };
  39. for (auto& p: persons)
  40. std::cout << p._first << ' ' << p._last << '\n';
  41. }