Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/functional/hash/examples/books.cpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 1.4 KB
Line 
1
2//  Copyright Daniel James 2005. Use, modification, and distribution are
3//  subject to 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 "./books.hpp"
7#include <boost/functional/hash.hpp>
8#include <cassert>
9
10// If std::unordered_set was available:
11//#include <unordered_set>
12
13// This example illustrates how to use boost::hash with a custom hash function.
14// For full details, see the tutorial.
15
16int main()
17{
18    library::book knife(3458, "Zane Grey", "The Hash Knife Outfit");
19    library::book dandelion(1354, "Paul J. Shanley", "Hash & Dandelion Greens");
20
21    boost::hash<library::book> book_hasher;
22    std::size_t knife_hash_value = book_hasher(knife);
23
24    // If std::unordered_set was available:
25    //
26    //std::unordered_set<library::book, boost::hash<library::book> > books;
27    //books.insert(knife);
28    //books.insert(library::book(2443, "Lindgren, Torgny", "Hash"));
29    //books.insert(library::book(1953, "Snyder, Bernadette M.",
30    //    "Heavenly Hash: A Tasty Mix of a Mother's Meditations"));
31
32    //assert(books.find(knife) != books.end());
33    //assert(books.find(dandelion) == books.end());
34}
35
36namespace library
37{
38    bool operator==(book const& a, book const& b)
39    {
40        return a.id == b.id;
41    }
42
43    std::size_t hash_value(book const& b)
44    {
45        boost::hash<int> hasher;
46        return hasher(b.id);
47    }
48}
Note: See TracBrowser for help on using the repository browser.