如何更好地表示 6 个整数键而不是作为 6 维数组的索引?
How to represent 6 integer key better than as indices to a 6-dimensional array?
在我的程序中,状态可以由六个整数唯一标识。每个整数i
满足0 <= i <= 10
,并且每个状态都有一个关联的值。我目前正在使用一个 6 维数组来跟踪每个状态值。我将状态值存储在一个数组中,就像state[11][11][11][11][11][11][11]
一样,每个值都有 6 个整数作为键。但是,这个数组会非常稀疏,因为我只访问了少数可能的状态。有没有更好的方法来表示状态值的键?
如果你不需要在每个州都有一个值,你可以使用地图
概念代码,完全未经测试。
constexpr int dimension = 6;
using KeyType = std::array<char, dimension>;
int32_t Key(const & KeyType keys) {
int32_t res = 0;
for (auto key : keys) {
res <<= 4;
res += key;
}
return res;
}
void Key2Array(int32_t keyValue, KeyType& keys) {
int idx = dimension-1;
for (auto& key : keys) {
keys[idx--] = keyValue&0x16;
keyValue >>= 4;
}
}
std::map<int32_t, value> states;
states[Key({1,2,3,4,5,6}] = 42;
KeyType key;
Key2Array(0x123456, key);
相关文章:
- 特征获取索引数组,其中向量中的值为真(不需要循环)
- 是否可以使用字符串或字符索引数组
- 为什么在使用字符索引数组时会出现这种不同的行为
- 在具有重复索引的索引数组处更改 ArrayFire 数组
- 使用索引数组订购 ArrayFire Array 的最佳方式
- C++ 中结构的动态索引数组
- 1索引数组上的qsort()正在扰乱索引
- C++ 索引数组打印和删除字符串名称 数组打印
- 字符串下标超出范围.我不知道如何使用字符索引数组,所以我使用了(无符号整数),但它不起作用
- openGL drawElements - 一个额外的三角形,使用索引数组
- 具有索引数组的地形(高度贴图)LOD
- 通过创建索引数组进行 C++ 排序
- OpenGL:两个顶点数组 + 两个索引数组
- 将索引数组排序为主数组
- 自动循环跳过某些索引数组
- 为静态强制转换的索引数组生成数组
- 重载枚举索引数组的std::get
- 使用c++中的第二个索引数组对数组进行排序
- 给定的零索引数组 &该数组的均衡索引
- CUDA:重新索引数组