解决方案
对于C++,使用映射效果很好。数百万个对象将不是问题。一千万个项目在我的计算机上花费了大约4.4秒和大约57兆。
我的测试应用程序如下:
#include <stdio.h>
#include <stdlib.h>
#include <map>
class triple {
public:
int x;
int y;
int z;
bool operator<(const triple &other) const {
if (x < other.x) return true;
if (other.x < x) return false;
if (y < other.y) return true;
if (other.y < y) return false;
return z < other.z;
}
};
int main(int, char**)
{
std::map<triple,int> data;
triple point;
int i;
for (i = 0; i < 10000000; ++i) {
point.x = rand();
point.y = rand();
point.z = rand();
//printf("%d %d %d %d\n", i, point.x, point.y, point.z);
data[point] = i;
}
return 0;
}
现在要动态选择变量的数量,最简单的解决方案是将index表示为字符串,然后将string用作映射的键。例如,可以通过" 23,55"字符串表示位于[23] [55]的项目。我们还可以将此解决方案扩展到更大的尺寸;例如对于三个维度,任意索引将看起来像" 34,45,56"。此技术的简单实现如下:
std::map data<string,int> data; char ix[100]; sprintf(ix, "%d,%d", x, y); //2 vars data[ix] = i; sprintf(ix, "%d,%d,%d", x, y, z); //3 vars data[ix] = i;
日期:2020-03-24 20:29:59 来源:oir作者:oir
