• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 // Copyright 2006-2009 Daniel James.
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #include <boost/unordered_set.hpp>
7 #include <boost/functional/hash.hpp>
8 #include <boost/core/lightweight_test.hpp>
9 
10 //[point_example2
11     struct point {
12         int x;
13         int y;
14     };
15 
operator ==(point const & p1,point const & p2)16     bool operator==(point const& p1, point const& p2)
17     {
18         return p1.x == p2.x && p1.y == p2.y;
19     }
20 
hash_value(point const & p)21     std::size_t hash_value(point const& p) {
22         std::size_t seed = 0;
23         boost::hash_combine(seed, p.x);
24         boost::hash_combine(seed, p.y);
25         return seed;
26     }
27 
28     // Now the default function objects work.
29     boost::unordered_multiset<point> points;
30 //]
31 
main()32 int main() {
33     point x[] = {{1,2}, {3,4}, {1,5}, {1,2}};
34     for(int i = 0; i < sizeof(x) / sizeof(point); ++i)
35         points.insert(x[i]);
36     BOOST_TEST(points.count(x[0]) == 2);
37     BOOST_TEST(points.count(x[1]) == 1);
38     point y = {10, 2};
39     BOOST_TEST(points.count(y) == 0);
40 
41     return boost::report_errors();
42 }
43 
44