Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get hash code of a string in c++

Tags:

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.

like image 586
sufyan siddique Avatar asked Nov 11 '11 13:11

sufyan siddique


People also ask

How do you find the hash of a string?

Calculation of the hash of a stringhash ( s ) = s [ 0 ] + s [ 1 ] ⋅ p + s [ 2 ] ⋅ p 2 + . . .

What is hash value of a string?

A Hash function is a function that maps any kind of data of arbitrary size to fixed-size values. The values returned by the function are called Hash Values or digests. There are many popular Hash Functions such as DJBX33A, MD5, and SHA-256.

Is there a hash function in C?

A Hash Table in C/C++ (Associative array) is a data structure that maps keys to values. This uses a hash function to compute indexes for a key. Based on the Hash Table index, we can store the value at the appropriate location.


2 Answers

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

std::hash<std::string>()("foo"); 
like image 79
Cat Plus Plus Avatar answered Sep 23 '22 02:09

Cat Plus Plus


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"); } 
like image 27
tune2fs Avatar answered Sep 20 '22 02:09

tune2fs