簡體   English   中英

如何用C ++編寫此代碼

[英]how write this code in c++

我有以下代碼

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 ==========

包括<cstdint> (編譯器必須支持C ++ 0x)並更改以下數據類型:

  • uint32 > uint32_t (或std::uint32_t
  • uchar > unsigned char

正如評論中提到的,cstdint並非在所有編譯器中都可用,但是大多數時候您可以使用stdint.h (普通的C99頭文件,不在std名稱空間中)。

如果用“ C ++中的等效代碼”表示要重構為使用僅在C ++中可用的語言構造(例如模板或類),我會說沒有。

函數中沒有明顯的不變性,因此它實際上並不是構成對象的基礎。

要處理的數據的性質無法擴展為任意類型,因此模板似乎也不適合。

我認為建議使用stdint.h的評論是非常明智的建議,我還將用更隱含的有意義的內容(int const等)代替那些魔術數字。

將它們添加到代碼的頂部:

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

在我看來,那似乎已經是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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM