![]() |
Home | Libraries | People | FAQ | More |
boost::hash 通過調用函數 hash_value 生效。不指定名字空間,它就能通過參數依賴查找探測重載。所以,如果有一個自由函數 hash_value 和一個定制類型在同一個名字空間中,它就可以被調用。
如果你有一個結構 library::book,每一個 book 通過它的成員 id 來唯一確定:
namespace library { struct book { int id; std::string author; std::string title; // .... }; bool operator==(book const& a, book const& b) { return a.id == b.id; } }
那麼你需要做的全部就是編寫函數 library::hash_value:
namespace library
{
std::size_t hash_value(book const& b)
{
boost::hash<int> hasher;
return hasher(b.id);
}
}
現在你就可以使用 book 的 boost::hash 了:
library::book knife(3458, "Zane Grey", "The Hash Knife Outfit"); library::book dandelion(1354, "Paul J. Shanley", "Hash & Dandelion Greens");boost::hash<library::book> book_hasher; std::size_t knife_hash_value = book_hasher(knife); // If std::unordered_set is available: std::unordered_set<library::book,boost::hash<library::book> > books; books.insert(knife); books.insert(library::book(2443, "Lindgren, Torgny", "Hash")); books.insert(library::book(1953, "Snyder, Bernadette M.", "Heavenly Hash: A Tasty Mix of a Mother's Meditations")); assert(books.find(knife) != books.end()); assert(books.find(dandelion) == books.end());
完整的例子可以在 /libs/functional/hash/examples/books.hpp 和 /libs/functional/hash/examples/books.cpp 找到。
![]() |
提示 |
|---|---|
在寫散列函數時,首先看一下判斷相等的函數如何工作。相等的對象必須生成相同的散列值。如果對像不相等,它們就應該生成不同的散列值。在這個對象裡相等僅僅基於 id,所以散列函數僅僅散列了 id。如果它基於對像名和作者,那麼散列函數就應該考慮它們(如何做到這一點在下一部分討論)。 |