我试图从C代码转换djb2哈希函数
unsigned long
hash(unsigned char *str)
{
unsigned long hash = 5381;
int c;
while (c = *str++)
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
Run Code Online (Sandbox Code Playgroud)
到C ++代码,但我有分段错误。
int hf(std::string s){
unsigned long hash = 5381;
char c;
for(int i=0; i<s.size(); i++){
c=s[i++];
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
}
return hash;
Run Code Online (Sandbox Code Playgroud)
我的错误在哪里?提前致谢
我想更改 std::map (它是哈希函数),以便在迭代时以插入的方式返回对。我尝试过使用无序地图,但没有成功。所以我想我必须创建一个哈希函数,该函数每次都会递增并返回更大的值。我怎样才能做到这一点?我不使用大数据,所以性能不是问题。
由于我没有任何很好的数学技能,我问你是否有任何算法我应该用于一个可能在将来改变的课程.
考虑以下场景:
"角色"类包含以下字段:
private boolean admin;
private boolean printer;
Run Code Online (Sandbox Code Playgroud)
几个星期后,我决定添加一个角色"客人":
private boolean admin;
private boolean printer;
private boolean guest;
Run Code Online (Sandbox Code Playgroud)
几周后我决定删除角色"打印机";
private boolean admin;
private boolean guest;
Run Code Online (Sandbox Code Playgroud)
由于我将在数据库中保留哈希码,因此我必须100%确定此类的所有版本都生成唯一的哈希码.
也许这不是问题,我一直使用Eclispe IDE源代码生成器中提供的那个.
你能告诉我,如果我对Eclipse IDE(Indigo)Java版本> = 6方法安全,或者给我一些关于这个主题的其他建议.我相信这是一件很平常的事情.
提前致谢
我试图使用编程语言提供的默认哈希函数生成三个不同的字符串 A、B 和 C,以便它们的哈希值全部相等。具体来说,我需要确保A不等于B,B不等于C,A不等于C。
我尝试了多种方法,但尚未成功找到解决方案。我正在寻求帮助来实现可以满足这些要求的方法或算法。所有三个字符串的哈希值必须相同,这一点至关重要。
这是我的实现,但是它仍然不完整,因为我与前两个字符串发生了冲突,但与第三个字符串没有发生冲突。
var dictionary = new Dictionary<int, string>();
int collusionCounter = 0, stringCounter = 0;
string myString;
int hash = 0;
List<string> myList = new List<string>();
while (true)
{
stringCounter++;
myString = stringCounter.ToString();
try
{
hash = myString.GetHashCode();
dictionary.Add(hash, myString);
}
catch (Exception)
{
if (dictionary.ContainsKey(hash))
{
myList.Add(myString);
collusionCounter++;
if (collusionCounter == 2)
{
break;
}
}
continue;
}
}
var A = myList[0];
var B = myList[1];
var C = dictionary[hash];
Console.WriteLine($"{A.GetHashCode()} {B.GetHashCode()} {C.GetHashCode()}");
Run Code Online (Sandbox Code Playgroud)
hier …
我想问一下Hash Function中碰撞的概率?
谢谢
c++ ×2
hash ×2
hashcode ×2
c# ×1
cryptographic-hash-function ×1
cryptography ×1
hashmap ×1
java ×1
math ×1