开发者

how write this code in c++

i have following code

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;
}

what will be it's equivalent code in c++? i means data types

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
开发者_如何学C1>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 ==========


Include <cstdint> (compiler must support C++0x) and change following datatypes:

  • uint32 -> uint32_t (or std::uint32_t)
  • uchar -> unsigned char

As mentioned in comment, cstdint won't be available yet in all compilers, most of the time you can use stdint.h however (the normal C99 header, not in std namespace).


Add these to the top of your code:

typedef unsigned char uchar;
typedef unsigned long uint32;  // likely, but not guarenteed.
//typedef unsigned int size_t;  // should be defined in stdlib.h


If by "equivalent code in C++" you mean if this should be refactored to use language constructs only available in C++ such as templates or classes, I would say it doesn't.

There are no obvious invariants in the function so it doesn't really form the basis of an object.

The nature of the data to be processed doesn't scale to arbitrary types so templates don't seem a good fit either.

I think the comment suggesting the use of stdint.h is very sensible advice and I would also replace those magic numbers with something more implicitly meaningful (int const's, etc.).


That, to me, seems to already be a C++ code. I wonder why you think it's not.
But since C++ doesn't have uint and uchar explicitly defined you'll need to either have this piece of code before that function.
typedef unsigned int uint32;
typedef unsigned char uchar;

Otherwise, rewrite the function like:

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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜