我如何内存映射STD:在C图型结构或字符串的键值++?这是可以这样做?现在,我在做内存映射与键值INT的地图,但由于INT的最大范围是有限的,我在程序的键值的最大尺寸大约是10 ^ 24,我是被卡住。所以通过使用任一键类型结构或串我可以解决这个问题?
int p0, p1, p2;
map<int,int> p0obj, p1obj, p2obj, size_p; int count = 0;
float d0, d1, d2;
float a0 = a[p0];
float b0 = b[p0];
float a1 = a[p1];
float b1 = b[p1];
float a2 = a[p2];
float b2 = b[p2];
if(d0>0 && d1>0 && d2>0) {
int key = d0+max_d*(d1+max_d*(d2+max_d*(a0+max_c*(b0+max_c*(a1+max_c*(b1+max_c*(a2+max_c*b2)))))));
//std::string key = std::to_string(k);
p0obj[key] = p0; p1obj[key] = p1; p2obj[key] = p2; size_p[key]++;
oa << p0obj; oa << p1obj; oa << p2obj; oa << size_p;
std::cout<<"key="<<key<<std::endl;
}
}
}
看来你想用一堆float
s的作为地图的关键。
您可以通过包装它们作为tuple
做,要么或定义一个更有意义的struct
类型:
#include <map>
struct MyKey {
float d0, d1, d2, a0, b0, a1, b1, a2, b2;
bool operator < (const MyKey& o) const {
return std::tie(d0, d1, d2, a0, b0, a1, b1, a2, b2) < std::tie(o.d0, o.d1, o.d2, o.a0, o.b0, o.a1, o.b1, o.a2, o.b2);
}
};
struct MyValue {
float p0, p1, p2;
};
std::map<MyKey, MyValue> pobj;
要插入此地图:
pobj.insert({{d0, d1, d2, a0, b0, a1, b1, a2, b2}, {p0, p1, p2}});
但是不知道你会用这样的地图做什么。通过花车搜索可能不会进行得很顺利。但是看着了例如一个子空间lower_bound
可能仍然是有用的。
auto it = pobj.lower_bound({d0, d1, d2, a0, b0, a1, b1, a2, b2});
if (it != end(pobj)) {
const MyKey& key = it->first;
const MyValue& value = it->second;
std::cout << "found: " << value.p0 << " " << value.p1 << " " << value.p2 << std::endl;
}