XOR BITSET将2D焦点存储为1D
XOR bitset when 2D bitset is stored as 1D
在您只关心速度时回答如何存储二进制数据?我想编写一些进行比较,所以我想使用std::bitset
。但是,为了进行公平的比较,我希望1D std::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数据结构。
问题是 v[i * D]
访问一个位。在您的2D位数组的概念模型中,它访问了ROW i
和列0
的位。
因此,v[i * D]
是bool
,q
是std::bitset<D>
,而将其应用于这些的位逻辑XOR运算符(^
)是没有意义的。
如果v
代表D
大小的二进制向量序列,则应使用std::vector<std::bitset<D>>
。另外,std::bitset<N>::set()
将所有位设置为1
。
#include <vector>
#include <iostream>
#include <random>
#include <cmath>
#include <numeric>
#include <bitset>
int main()
{
const int N = 1000000;
const int D = 100;
std::vector<std::size_t> hamming_dist(N);
std::bitset<D> q;
q.set();
std::vector<std::bitset<D>> v(N);
for (int i = 0; i < N; ++i)
{
v[i].set();
}
for (int i = 0; i < N; ++i)
{
hamming_dist[i] = (v[i] ^ q).count();
}
std::cout << "hamming_distance = " << hamming_dist[0] << "n";
return 0;
}
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 将字符串存储在c++中的稳定内存中
- std::原子加载和存储都需要吗
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- C++:将控制台输出存储在宏中更好吗
- C++使用数组作为多维数组,尽管将其初始化为带有指针的 1D
- 使用QProcess执行命令,并将结果存储在QStringList中
- 访问存储在向量C++中的结构的多态成员
- 如何从存储在std::映射中的std::集中删除元素
- 存储模板类型以强制转换回派生<T>
- 类型总是使用其大小存储在内存中吗
- 当字符串存储在变量中时,如何将字符串转换为wchar_t
- 访问表示 2D 矩阵的 1D 存储数组
- 如何将元素的索引从2D数组存储到1D数组中,然后交换这些值
- 2D阵列和1D阵列存储比较
- XOR BITSET将2D焦点存储为1D
- 如何在AVR(例如:ATMega)或Arduino微控制器上制作2D PROGMEM阵列的1D PROGMEM数组(存储
- 高效访问存储为 1D 阵列的 3D 阵列
- 大型3D阵列的性能:连续1D存储与T***
- 将1D阵列存储到2D