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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
sufyan siddique
  • 1,461
  • 4
  • 13
  • 14

6 Answers6

75

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

std::hash<std::string>()("foo");
Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
  • 5
    `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`, defined in ``. – user4815162342 Sep 21 '12 at 09:03
  • 1
    @user4815162342 "boost::hash is not C++03, but boost." Oh, right, how could I forget about C++Boost. – Cat Plus Plus Sep 21 '12 at 10:25
  • I'm not sure if you're being ironic, but just to clarify: I have worked in C++ environments where the use of `boost` was forbidden because of logistical reasons, such as the complications of integrating the boost build system with the application or environment limitations on anything "external" and "new". These same environments typically did support the C++03 standard library with the tr1 extensions, although it was often the case that no one was aware of them. – user4815162342 Sep 21 '12 at 10:29
  • Yes, there are places where you can't get away with using Boost. But it's still just a C++ library, implemented in C++. Most companies allow the use of at least *some* third-party libraries, and there's really nothing special about Boost there. Sometimes it's allowed, sometimes it's not. But it's certainly not some kind of "special dialect" of C++. `boost::hash` is C++03. It is not in the C++03 standard library, but it is written 100% in C++03, it compiles under C++03 and it is usable from C++03 code. – jalf Sep 21 '12 at 11:09
  • @user4815162342, you know, there are some people who *write in C*. So what? It doesn't mean we should use such insane practices. – Abyx Sep 21 '12 at 11:09
  • 1
    @user4815162342 FWIW, for a header-only library like this, you can just [bcp](http://www.boost.org/doc/libs/1_51_0/tools/bcp/doc/html/index.html) the hell out of it and drop it in your project tree. There are no technical or logistical reasons to avoid it. Only political reasons. – R. Martinho Fernandes Sep 21 '12 at 11:11
  • `tr1` depends on compiler availability. `boost` is *always* available, even if it may not be usable for political reasons in some environments. – jalf Sep 21 '12 at 11:11
  • 4
    @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. – user4815162342 Sep 21 '12 at 11:28
  • 4
    No, I just don't care about politics. – Cat Plus Plus Sep 21 '12 at 11:30
  • 3
    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. :) – jalf Sep 21 '12 at 12:08
  • @user4815162342 CatPlusPlus never said that Boost was part of C++-30, just that if you are using C++03 you can use a solution provided by boost, if you are using C++11 you can use a standard library function. – thecoshman Jan 13 '14 at 10:10
15

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");
}
tune2fs
  • 7,605
  • 5
  • 41
  • 57
6

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;
   }
  • 3
    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() – thecoshman Jan 13 '14 at 10:12
  • 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++ ? – Bitterblue Jul 16 '14 at 14:21
4

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

Megatron
  • 2,871
  • 4
  • 40
  • 58
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;
}
Aero
  • 77
  • 6
  • The reason not to use std::hash or boost hash is we want the same return value as java.util.String.HashCode. – Aero Jul 24 '19 at 03:07
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; 
}
  • 3
    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 Jan 13 '14 at 10:15
  • 1
    @thecoshman because sometimes you want to be able to come up with the same hashes in different languages – James Jan 13 '16 at 15:29