What is the hash function that is used in hash_map / hash_set

Y

yuyang08

Hello, everyone,

I am wondering what is the hash function that is used in
hash_map/hash_set. Can I replace it with my own
hash function? Any comments on this?

Thanks!

-Andy
 
P

Pete Becker

I am wondering what is the hash function that is used in
hash_map/hash_set. Can I replace it with my own
hash function? Any comments on this?

By default, hash_map and hash_set use

namespace std {
namespace tr1 {
template <class Ty> struct hash
: unary_function<Ty, size_t>
{
size_t operator()(Ty val) const;
};
} }

This template is specialized for bool, all integer types, all
floating-point types, pointers, std::string, and std::wstring.

You can specialize it for any of your own types.

You can also use your own type as an argument to hash_map or hash_set.

For details, see section 5.6 of my book, "The C++ Standard Library
Extensions: a Tutorial and Reference."
 
T

Thomas Tutone

Pete said:
By default, hash_map and hash_set use

namespace std {
namespace tr1 {
template <class Ty> struct hash
: unary_function<Ty, size_t>
{
size_t operator()(Ty val) const;
};
} }

This template is specialized for bool, all integer types, all
floating-point types, pointers, std::string, and std::wstring.

You can specialize it for any of your own types.

You can also use your own type as an argument to hash_map or hash_set.

For details, see section 5.6 of my book, "The C++ Standard Library
Extensions: a Tutorial and Reference."

If I'm not mistaken, section 5.6 of your book deals with the hash
function object for std::tr1::unordered_map and
std::tr1::unordered_set. hash_map and hash_set, which the OP refers
to, are implementation-specific extensions (from SGI, perhaps?), not
the new std::tr1 containers.

Best regards,

Tom
 
P

Pete Becker

Thomas said:
If I'm not mistaken, section 5.6 of your book deals with the hash
function object for std::tr1::unordered_map and
std::tr1::unordered_set. hash_map and hash_set, which the OP refers
to, are implementation-specific extensions (from SGI, perhaps?), not
the new std::tr1 containers.

You're right. Sorry for any confusion this caused.
 
P

Pete Becker

Pete said:
You're right. Sorry for any confusion this caused.

I should add, though, that the specifications in TR1 for unordered_set
and unordered_map were designed to be compatible with existing
implementations of hash_set and hash_map. The Dinkumware implementations
of hash_set, hash_map, unordered_set, and unordered_map all share the
same underlying code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top