#include<bits/stdc++.h> using namespace std; unsigned int getBlkno(string &input_data,int idx){ string s = input_data.substr(idx*4,4); unsigned int res = 0; for(int i=0;i<4;i++){ res += s[i]<<(24-i*8); } return res; } unsigned int Murmurhash3(string input_data,unsigned int seed){ unsigned int h = seed; //声明常量 const unsigned int c1 = 0xcc9e2d51; // 3,432,918,353 const unsigned int c2 = 0x1b873593; // 461,845,907 const int r1 = 15; const int r2 = 13; const int m = 5; const int n = 0xe6546b64; //3,864,292,196 //分块处理 int blkNums = input_data.size()/4; //1.一个块一个块地处理,这是第一部分地工作 for(int i=0;i<blkNums;i++){ unsigned int K = getBlkno(input_data,i); K *= c1; K = _rotl(K,r1); K *= c2; K = _rotl(K,r2); h = h*m + n; } //2.处理剩余量 string remaining_bytes = input_data.substr(blkNums*4); unsigned int k = 0; switch (remaining_bytes.size()){ case 3:k^=remaining_bytes[2]<<16; case 2:k^=remaining_bytes[1]<<8; case 1:k^=remaining_bytes[0]; } k = k * c1; k = _rotl(k,r1); k = k * c2; h ^= k; h ^= input_data.size(); //3.加强雪崩测试 h ^= h >> 16; h *= 0x85ebca6b; // 2,246,822,507 h ^= h >> 13; h *= 0xc2b2ae35; // 3,266,489,909 h ^= h >> 16; return h; } int main(){ string input_data = "Jack"; int seed = 123; cout<<"Murmurhash3(Jack,123) = "<<Murmurhash3(input_data,seed); system("pause"); }
时间: 2024-04-23 09:26:51 浏览: 11
这段代码中有一些问题,需要进行修改:
1. 代码开头应该包含头文件 `#include<iostream>`,否则无法使用 `cout` 和 `system` 函数。
2. 在 `getBlkno` 函数中,需要将 `s[i]` 强制转换为 `unsigned int` 类型,否则会出现类型不匹配的错误。
3. 在 `Murmurhash3` 函数中,需要将 `h` 和 `K` 声明为 `unsigned int` 类型,否则会出现类型不匹配的错误。
4. 在 `Murmurhash3` 函数中,需要将 `_rotl` 函数改为 `_rotr` 函数,否则会出现未定义的错误。
5. 在 `Murmurhash3` 函数中,需要将 `h ^= h >> 16` 改为 `h ^= h >> 13`,否则会出现错误的哈希值。
修改后的代码如下:
相关问题
#include <bits/stdc++.h> using namespace std;
这个头文件是C++11标准引入的,它包含了所有标准库中的头文件。使用这个头文件可以方便地在一个地方包含所有需要的头文件,而不需要一个一个地包含。这个头文件通常只在竞赛中使用,因为它不是标准C++头文件,不保证在所有编译器中都能正常工作。
以下是一个使用这个头文件的示例,实现输入4个整数a、b、c、d,将它们倒序输出:
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
cout << d << ' ' << c << ' ' << b << ' ' << a << endl;
return 0;
}
```
#include <bits/stdc++.h> using namespace std
这段代码是一个C++的头文件引用和命名空间的使用示例。具体来说,`#include <bits/stdc++.h>`是一个常用的头文件引用方式,它包含了C++标准库中的所有头文件。而`using namespace std`则是为了使用`std`命名空间中的标准库函数和对象,这样就可以直接使用`cout`、`cin`等标准输入输出流对象,而不需要写`std::cout`、`std::cin`。
这种写法虽然方便,但也存在一些问题。首先,包含了所有的标准库头文件可能会导致编译时间变长。其次,使用了`using namespace std`会将整个`std`命名空间中的所有标识符引入当前作用域,可能会导致命名冲突。因此,在实际开发中,建议根据需要只包含需要的头文件,并使用具体的命名空间来避免潜在的问题。