23

Following java code returns hash code of a string.

String uri = "Some URI"
public int hashCode() {
    return uri.hashCode();
}

I want to translate this code to c++. Is there any function availabe in c++ or an easy way to translate this.

1
  • 5
    What do you need the hash for? This is important for the answer. Commented Nov 11, 2011 at 13:49

6 Answers 6

75

In C++03, boost::hash. In C++11, std::hash.

std::hash<std::string>()("foo");
Sign up to request clarification or add additional context in comments.

11 Comments

boost::hash is not C++03, but boost. In environments that support tr1 (much more widely available than C++11), you can use std::tr1::hash<T>, defined in <tr1/functional>.
@user4815162342 "boost::hash is not C++03, but boost." Oh, right, how could I forget about C++Boost.
@jalf It was never my intention to imply that boost is a dialect of C++, just that boost::hash is not a part of C++03 the way, for example, std::string is. I also pointed out that std::tr1::hash is an alternative available in some environments where boost isn't. It was a well-intentioned suggestion to improve an otherwise good answer, at which Cat Plus Plus unfortunately took offense.
No, I just don't care about politics.
but tr1::hash is not part of C++03 either. Of course, you're right that it's useful to mention both the tr1 and boost solutions, because someone might be able to use one or the other but not both. but originally you presented it as if tr1 was somehow "more C++03" than boost is, which is nonsense. :)
|
14

Boost provides a hash function:

boost hash

#include <boost/functional/hash.hpp>

int hashCode()
{
    boost::hash<std::string> string_hash;

    return string_hash("Hash me");
}

2 Comments

Its a header only library no need to build all of boost.
Feels a bit over the top if you can just use std::hash instead. What is the advantage of boost?
5

The following is the source for the default String.hashCode() in Java, this is a trival exercise to implement in C++.

public int hashCode()  
{
       int h = hash;
       if (h == 0 && count > 0) 
       {
           int off = offset;
           char val[] = value;
           int len = count;

           for (int i = 0; i < len; i++) 
           {
               h = 31*h + val[off++];
           }
           hash = h;
       }
       return h;
   }

2 Comments

well... a bit of an arse backwards way to go about it... I guess this is useful if want your hashing to be compatible with Java String.hashCode()
If you initiate the values correctly with 0 and string length it delivers the same values as Java's string. But I see the "common agreement" is there is no default function to use in C/C++ ?
3

Personally, I like to use boost's hash functions

http://www.boost.org/doc/libs/1_47_0/doc/html/hash.html

making a string hash is pretty simple,

boost::hash<std::string> string_hash;

std::size_t h = string_hash("Hash me");

newer versions of C++ have an equivalent with std::hash

Comments

2

I encoutered the same question as you have, hope this code will help you :

int HashCode (const std::string &str) {
    int h = 0;
    for (size_t i = 0; i < str.size(); ++i)
        h = h * 31 + static_cast<int>(str[i]);
    return h;
}

1 Comment

The reason not to use std::hash or boost hash is we want the same return value as java.util.String.HashCode.
1

//For C++ Qt you can use this code, the result is the sames as for Java hashcode()

int  hashCode(QString text){
    int hash = 0, strlen = text.length(), i;
    QChar character;
    if (strlen == 0)
        return hash;
    for (i = 0; i < strlen; i++) {
        character = text.at(i);
        hash = (31 * hash) + (character.toAscii());
    }
    return hash; 
}

2 Comments

Where does this random code come from? How do I know this will actually produce good quality hashes? Why on earth are suddenly assuming Qt is at play? Why are we writing code from scratch and not using a provided solution?
@thecoshman because sometimes you want to be able to come up with the same hashes in different languages

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.