473,474 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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

Sep 2 '06 #1
4 4886
yu******@gmail.com wrote:
>
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 Tystruct 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."
Sep 2 '06 #2
Pete Becker wrote:
yu******@gmail.com wrote:

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 Tystruct 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

Sep 3 '06 #3
Thomas Tutone wrote:
>
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.
Sep 3 '06 #4
Pete Becker wrote:
Thomas Tutone wrote:
>>
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.
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.
Sep 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Murali | last post by:
I have a requirement where I have to use two unsigned ints as a key in a STL hash map. A couple of ways to do this is 1. create a struct with two unsigned ints and use that as key (write my own...
4
by: Pelo GANDO | last post by:
Hi everybody ! I am a beginner in C++. I am looking for a (simple if it's possible) source code in C++ about hash table... Thank you ! Pelo
2
by: rolivawdaneel | last post by:
Hello, I am using SGI's hash_map in such a way that there is an inherent memory leak. Let me explain: 1- My hash_map takes char* as keys and one of my class, say myClass, as data. I give the...
3
by: Markus Dehmann | last post by:
I have a class "Data" and I store Data pointers in an STL set. But I have millions of inserts and many more lookups, and my profiler found that they cost a lot of runtime. Therefore, I want to...
7
by: Diane | last post by:
Hi everybody. Does anyone have any sample hash table implementation for separate chaining? I'm trying to implement it myself, but I'm stuck. Thanks!
3
by: railrulez | last post by:
Hi, Attached is a program which uses a hash_set, but I cant seem to get find() or iterators working on it. I'm not sure whether hash_set is std C++, but I dont know where else to ask. ...
1
by: Mike | last post by:
Hi, I have a multidimensional array that I would like to access as a hash map for performance reasons. I cannot see anything in the STL (I mean STL/TR1) that supports multi-dimensional hash...
11
by: xz | last post by:
>From the reference of MSDN about hash map: operator Inserts an element into a hash_map with a specified key value. And such example is given: hash_map <int, inthm1; hm1 = 40;
0
by: ilknur | last post by:
Hi,i have the following code to find the frequences of same words in a file.i used hashmap for this purpose but when i run the code, it enters a infinite loop.when i debug the code,i see that the...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.