15

Does the STL have any Hash functions available, that are exposed publicly?

I know that there are some non-standard implementations that use hash values (such as boost::hash_map), and MSVC8 implements a version of the hash_map/hash_set/etc.

But are there any Hash Functions that are defined in the C++98 STL?

If not, what are the best non-C++98 sources of a reliable hash function?

Order of preferred sources (from most acceptable to least): Boost, C++0x standard STL, TR1, other 3rd party.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
J T
  • 4,946
  • 5
  • 28
  • 38

3 Answers3

16

to summarize:

And all of them are designed for hashed associative containers, not for cryptography.

Cubbi
  • 46,567
  • 13
  • 103
  • 169
  • 3
    I believe that the first point is incorrect. STL does NOT have hash functions. The link you posted to SGI's STL implementation specifically quotes "This class is an SGI extension; it is not part of the C++ standard" – J T Apr 13 '11 at 17:57
  • Since the C++98/TR1/11 standard libary is also STL, it would be more accruate to say that the SGI STL implementation has them, the TR1 STL has them, the C++0x/11 implementation has them, and some compilers have and STL implementations them (IIRC, VC++ had them for quite a time, and so did STLport). They seem more or less interchangeable, but there might be fine details where they differ, so you should really look for a C++0x conformant implementation for maximum compatibility. – Boaz Yaniv Apr 13 '11 at 18:09
  • 2
    @J T: that's a bit of backwards-looking. The (HP/SGI) STL existed before C++98 and had hash functions back then. Not all parts of the STL made it into C++98; SGI subsequently documented those as "extensions to the standard". See Matt Austern's STL book for more details. – MSalters Apr 13 '11 at 18:13
  • @J T @Boaz Yaniv never in C++98/TR1/11 is the abbreviation STL used, or the words "template library" or anything of that kind. – Cubbi Apr 13 '11 at 18:21
  • @Boaz, @MSalters: hmm interesting. Thank-you for that info. – J T Apr 13 '11 at 18:42
  • @Cubbi you could say that, but when people talk about the STL, they usually mean the C++ standard library, and not the venerable SGI or HP implementations that went under that name. It used to be different, but how many people are using the SGI STL or even STLport now? – Boaz Yaniv Apr 13 '11 at 19:44
  • 1
    @Boaz Yaniv: here's a good answer to this: http://stackoverflow.com/questions/5205491/whats-this-stl-vs-c-standard-library-fight-all-about/5205571#5205571 – Cubbi Apr 13 '11 at 20:02
  • 1
    I note that you're referencing N3242 for C++11. Just today the final C++11 draft became available, N3290: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3290.pdf . – Howard Hinnant Apr 13 '11 at 23:46
3

I guess you're looking for hash functions for hash tables, not for cryptography, correct?

In that case, what about boost::hash?

The documentation says it's compatible with the TR1 hash, which should become part of the upcoming C++0x standard. That means it's probably already found in quite a few compilers.

For cryptographic hashes, there seems to be a SHA-1 implementation in Boost, but the way to go if you need them heavyweight is to use a dedicated library, such as Crypto++.

Boaz Yaniv
  • 6,334
  • 21
  • 30
  • Actually, this is for Crytographic reasons. I'll update the tags for the question. – J T Apr 13 '11 at 17:28
  • Well, if you're already using boost it seems to has [this](http://www.boost.org/doc/libs/1_46_1/boost/uuid/sha1.hpp). Otherwise you can use a cryptography library like [Crypto++](http://www.cryptopp.com/) – Boaz Yaniv Apr 13 '11 at 17:32
  • That SHA-1 is in `detail` namespace, meaning it's not intended to be used outside `boost.uuids`. We have to wait for boost.crypto which is probably waiting for boost.mp_math – Cubbi Apr 13 '11 at 17:47
  • Or we can just use a separate library for cryptography. I know I do. – Boaz Yaniv Apr 13 '11 at 17:50
0

The choice of Hash function ideally depends on your use for the results. I suspect this may partly be due to the idea "one size does not fit all".

Jay
  • 13,803
  • 4
  • 42
  • 69