我有以下代码
uint32 joaat_hash(uchar *key, size_t len)
{
uint32 hash = 0;
size_t i;
for (i = 0; i < len; i++)
{
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
它在c ++中的等效代码是什么?我的意思是数据类型
1>------ Build started: Project: hash_functions, Configuration: Debug Win32 ------
1> hash_function.cpp
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2146: syntax error : missing ';' before identifier 'joaat_hash'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2065: 'uchar' : undeclared identifier
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2065: 'key' : undeclared identifier
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2275: 'size_t' : illegal use of this type as an expression
1> c:\users\david\documents\visual studio 2010\projects\hash_functions\predefined c++ types (compiler internal)(19) : see declaration of 'size_t'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2146: syntax error : missing ')' before identifier 'len'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2078: too many initializers
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2275: 'size_t' : illegal use of this type as an expression
1> c:\users\david\documents\visual studio 2010\projects\hash_functions\predefined c++ types (compiler internal)(19) : see declaration of 'size_t'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(4): error C2143: syntax error : missing ';' before '{'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(4): error C2447: '{' : missing function header (old-style formal list?)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
答案 0 :(得分:5)
包括<cstdint>
(编译器必须支持C ++ 0x)并更改以下数据类型:
uint32
- &gt; uint32_t
(或std::uint32_t
)uchar
- &gt; unsigned char
正如评论中所提到的,cstdint在所有编译器中都不可用,但大多数情况下你可以使用stdint.h
(普通的C99头,而不是std命名空间)。
答案 1 :(得分:2)
如果通过“C ++中的等效代码”,你的意思是,如果要重构这一点,以便使用仅在C ++中可用的语言结构,例如模板或类,我会说它不会。
函数中没有明显的不变量,因此它并不真正构成对象的基础。
要处理的数据的性质不会扩展到任意类型,因此模板似乎也不合适。
我认为建议使用stdint.h
的评论是非常明智的建议,我也会用更隐含有意义的东西(int const's等)替换那些神奇的数字。
答案 2 :(得分:1)
将这些添加到代码顶部:
typedef unsigned char uchar;
typedef unsigned long uint32; // likely, but not guarenteed.
//typedef unsigned int size_t; // should be defined in stdlib.h
答案 3 :(得分:0)
对我而言,似乎已经是C ++代码了。我想知道为什么你认为不是。
但是由于C ++没有uint和uchar明确定义,你需要在该函数之前使用这段代码。
typedef unsigned int uint32;
typedef unsigned char uchar;
否则,重写函数如:
unsigned int joaat_hash(unsigned char *key, size_t len)
{
unsigned int hash = 0;
size_t i;
for (i = 0; i < len; i++)
{
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}