books.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2005-2009 Daniel James.
  2. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #include "./books.hpp"
  5. #include <boost/container_hash/hash.hpp>
  6. #include <cassert>
  7. // If std::unordered_set was available:
  8. //#include <unordered_set>
  9. // This example illustrates how to use boost::hash with a custom hash function.
  10. // For full details, see the tutorial.
  11. int main()
  12. {
  13. library::book knife(3458, "Zane Grey", "The Hash Knife Outfit");
  14. library::book dandelion(1354, "Paul J. Shanley", "Hash & Dandelion Greens");
  15. boost::hash<library::book> book_hasher;
  16. std::size_t knife_hash_value = book_hasher(knife);
  17. (void)knife_hash_value; // suppress unused variable warning
  18. // If std::unordered_set was available:
  19. //
  20. //std::unordered_set<library::book, boost::hash<library::book> > books;
  21. //books.insert(knife);
  22. //books.insert(library::book(2443, "Lindgren, Torgny", "Hash"));
  23. //books.insert(library::book(1953, "Snyder, Bernadette M.",
  24. // "Heavenly Hash: A Tasty Mix of a Mother's Meditations"));
  25. //assert(books.find(knife) != books.end());
  26. //assert(books.find(dandelion) == books.end());
  27. return 0;
  28. }
  29. namespace library
  30. {
  31. bool operator==(book const& a, book const& b)
  32. {
  33. return a.id == b.id;
  34. }
  35. std::size_t hash_value(book const& b)
  36. {
  37. boost::hash<int> hasher;
  38. return hasher(b.id);
  39. }
  40. }