开发者

Any built in hash method in C++?

I was looking for md5 for C++, and I realize md5 is not built in (even though there are a lot of very good libraries to support the md5 function). Then, I realized I don't actually need md5, any hashing method will do. Thus, I was wondering if C++ has such functions? I mean, built-in hashing functions?

While I was researching for C++, I saw Java, PHP, and some other programming languages support md5. For example, in PHP, you just need to call: md5("your string");.

A simple hash function will do. (If possible, please include some simple code on how to use i开发者_开发技巧t.)


This is simple. With C++11 you get a

hash<string>

functor which you can use like this (untested, but gives you the idea):

hash<string> h;
const size_t value = h("mystring");

If you don't have C++11, take a look at boost, maybe boost::tr1::hash_map. They probably provide a string-hashing function, too.

For very simple cases you can start with something along these lines:

size_t h = 0
for(int i=0; i<s.size(); ++i)
    h = h*31 + s[i];
return h;

To take up the comment below. To prevent short strings from clustering you may want to initialize h differently. Maybe you can use the length for that (but that is just my first idea, unproven):

size_t h = numeric_limits::max<size_t>() / (s.length()+1); // +1: no div-by-0
...

This should not be worse then before, but still far from perfect.


It depends which version of C++ you have... and what kind of hashing function you are looking for.

C++03 does not have any hashing container, and thus no need for hashing. A number of compilers have been proposing custom headers though. Otherwise Boost.Functional.Hash may help.

C++0x has the unordered_ family of containers, and thus a std::hash predicate, which already works for C++ standard types (built-in types and std::string, at least).

However, this is a simple hash, good enough for hash maps, not for security.

If you are looking for cryptographic hash, then the issue is completely different (and md5 is loosy), and you'll need a library for (for example) a SHA-2 hash.

If you are looking for speed, check out CityHash and MurmurHash. Both have restrictions, but they are heavily optimized.


How about using boost, Boost.Functional/Hash

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜