point2.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright 2006-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 <boost/unordered_set.hpp>
  5. #include <boost/functional/hash.hpp>
  6. #include <boost/core/lightweight_test.hpp>
  7. //[point_example2
  8. struct point {
  9. int x;
  10. int y;
  11. };
  12. bool operator==(point const& p1, point const& p2)
  13. {
  14. return p1.x == p2.x && p1.y == p2.y;
  15. }
  16. std::size_t hash_value(point const& p) {
  17. std::size_t seed = 0;
  18. boost::hash_combine(seed, p.x);
  19. boost::hash_combine(seed, p.y);
  20. return seed;
  21. }
  22. // Now the default function objects work.
  23. boost::unordered_multiset<point> points;
  24. //]
  25. int main() {
  26. point x[] = {{1,2}, {3,4}, {1,5}, {1,2}};
  27. for(int i = 0; i < sizeof(x) / sizeof(point); ++i)
  28. points.insert(x[i]);
  29. BOOST_TEST(points.count(x[0]) == 2);
  30. BOOST_TEST(points.count(x[1]) == 1);
  31. point y = {10, 2};
  32. BOOST_TEST(points.count(y) == 0);
  33. return boost::report_errors();
  34. }