mirror of
https://github.com/boostorg/functional.git
synced 2025-08-02 22:14:28 +02:00
Change the books hash example to have a separate header.
[SVN r28340]
This commit is contained in:
@@ -3,37 +3,16 @@
|
|||||||
// subject to the Boost Software License, Version 1.0. (See accompanying
|
// 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)
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
|
||||||
|
#include "./books.hpp"
|
||||||
#include <boost/functional/hash.hpp>
|
#include <boost/functional/hash.hpp>
|
||||||
// If we had std::unordered_set.
|
|
||||||
//#include <unordered_set>
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
|
// If std::unordered_set was available:
|
||||||
|
//#include <unordered_set>
|
||||||
|
|
||||||
// This example illustrates how to use boost::hash with a custom hash function.
|
// This example illustrates how to use boost::hash with a custom hash function.
|
||||||
// For full details, see the tutorial.
|
// 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()
|
int main()
|
||||||
{
|
{
|
||||||
library::book knife(3458, "Zane Grey", "The Hash Knife Outfit");
|
library::book knife(3458, "Zane Grey", "The Hash Knife Outfit");
|
||||||
@@ -42,7 +21,7 @@ int main()
|
|||||||
boost::hash<library::book> book_hasher;
|
boost::hash<library::book> book_hasher;
|
||||||
std::size_t knife_hash_value = book_hasher(knife);
|
std::size_t knife_hash_value = book_hasher(knife);
|
||||||
|
|
||||||
// If we had std::unordered_set:
|
// If std::unordered_set was available:
|
||||||
//
|
//
|
||||||
//std::unordered_set<library::book, boost::hash<library::book> > books;
|
//std::unordered_set<library::book, boost::hash<library::book> > books;
|
||||||
//books.insert(knife);
|
//books.insert(knife);
|
||||||
@@ -53,3 +32,17 @@ int main()
|
|||||||
//assert(books.find(knife) != books.end());
|
//assert(books.find(knife) != books.end());
|
||||||
//assert(books.find(dandelion) == 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<int> hasher;
|
||||||
|
return hasher(b.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
26
hash/examples/books.hpp
Normal file
26
hash/examples/books.hpp
Normal file
@@ -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 <cstddef>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
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&);
|
||||||
|
}
|
Reference in New Issue
Block a user