diff --git a/hash/examples/books.cpp b/hash/examples/books.cpp index 00d41fc..e91ebba 100644 --- a/hash/examples/books.cpp +++ b/hash/examples/books.cpp @@ -3,37 +3,16 @@ // subject to the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +#include "./books.hpp" #include -// If we had std::unordered_set. -//#include #include +// If std::unordered_set was available: +//#include + // This example illustrates how to use boost::hash with a custom hash function. // For full details, see the tutorial. -namespace library -{ - struct book - { - int id; - std::string author; - std::string title; - - book(int i, std::string const& a, std::string const& t) - : id(i), author(a), title(t) {} - }; - - bool operator==(book const& a, book const& b) - { - return a.id == b.id; - } - - std::size_t hash_value(book const& b) - { - return boost::hash_value(b.id); - } -} - int main() { library::book knife(3458, "Zane Grey", "The Hash Knife Outfit"); @@ -42,7 +21,7 @@ int main() boost::hash book_hasher; std::size_t knife_hash_value = book_hasher(knife); - // If we had std::unordered_set: + // If std::unordered_set was available: // //std::unordered_set > books; //books.insert(knife); @@ -53,3 +32,17 @@ int main() //assert(books.find(knife) != books.end()); //assert(books.find(dandelion) == books.end()); } + +namespace library +{ + bool operator==(book const& a, book const& b) + { + return a.id == b.id; + } + + std::size_t hash_value(book const& b) + { + boost::hash hasher; + return hasher(b.id); + } +} diff --git a/hash/examples/books.hpp b/hash/examples/books.hpp new file mode 100644 index 0000000..411b066 --- /dev/null +++ b/hash/examples/books.hpp @@ -0,0 +1,26 @@ + +// Copyright Daniel James 2005. Use, modification, and distribution are +// subject to the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// This example illustrates how to use boost::hash with a custom hash function. +// The implementation is contained in books.cpp + +#include +#include + +namespace library +{ + struct book + { + int id; + std::string author; + std::string title; + + book(int i, std::string const& a, std::string const& t) + : id(i), author(a), title(t) {} + }; + + bool operator==(book const&, book const&); + std::size_t hash_value(book const&); +}