回答只关心速度时如何存储二进制数据?,我正在尝试写一些来做比较,所以我想使用std::bitset
. 但是,为了公平比较,我希望 1Dstd::bitset
模拟 2D。
因此,而不是:
bitset<3> b1(string("010"));
bitset<3> b2(string("111"));
我想使用:
bitset<2 * 3> b1(string("010111"));
优化数据局部性。但是,现在我遇到了如何存储和计算二进制码之间的汉明距离的问题?,如我的最小示例所示:
#include <vector>
#include <iostream>
#include <random>
#include <cmath>
#include <numeric>
#include <bitset>
int main()
{
const int N = 1000000;
const int D = 100;
unsigned int hamming_dist[N] = {0};
std::bitset<D> q;
for(int i = 0; i < D; ++i)
q[i] = 1;
std::bitset<N * D> v;
for(int i = 0; i < N; ++i)
for(int j = 0; j < D; ++j)
v[j + i * D] = 1;
for(int i = 0; i < N; ++i)
hamming_dist[i] += (v[i * D] ^ q).count();
std::cout << "hamming_distance = " << hamming_dist[0] << "\n";
return 0;
}
错误:
Georgioss-MacBook-Pro:bit gsamaras$ g++ -Wall bitset.cpp -o bitset
bitset.cpp:24:32: error: invalid operands to binary expression ('reference' (aka
'__bit_reference<std::__1::__bitset<1562500, 100000000> >') and
'std::bitset<D>')
hamming_dist[i] += (v[i * D] ^ q).count();
~~~~~~~~ ^ ~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/bitset:1096:1: note:
candidate template ignored: could not match 'bitset' against
'__bit_reference'
operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
^
1 error generated.
这是因为它不知道何时停止!在处理 D 位后如何告诉它停止?
我的意思是不使用 2D数据结构。