在我的代码中,我有以下几行:
if(mymap.count(plan) == 0) {
std::vector<XYZ> v;
v.reserve(10);
mymap.emplace(plan, v);
std::cout << "Plan " << plan.normal << " @ " << plan.O << " added";
}
//I inserted this code for debugging
std::map<Plan, std::vector<XYZ>>::const_iterator it = mymap.find(plan);
if(it == this->intersections.end())
std::cout << "not found";
我怎样才能在控制台plan added
和not found
之后阅读?
我的地图声明如下:
std::map<Plan, std::vector<XYZ>, PlanComp> mymap;
在某些时候,我认为它来自比较器,但它尊重反射性,反对称性,传递性,等价的传递性(根据this blog就足够了):
struct PlanComp {
bool operator()(const Plan& l, const Plan& n) const {
return (l.O.x != n.O.x) || (l.O.y != n.O.y) || (l.O.z != n.O.z)
|| (l.normal.x != n.normal.x) || (l.normal.y != n.normal.y) || (l.normal.z != n.normal.z);
}
};
struct XYZ {
double x;
double y;
double z;
};
struct Plan {
XYZ O;
XYZ plan;
};
答案 0 :(得分:2)
您的比较器没有定义strict weak ordering(松散地说,“小于”定义元素顺序的语义)。因此,您的代码表现出未定义的行为。
最简单的解决方案是使用词典比较器 - 首先比较x
,然后仅在出现平局时比较y
,依此类推。在C ++ 11中,它甚至更简单;元组的operator <
已经为你做了这个(你可以用std::tie
来获取元组)。有关示例,请参阅Operator < and strict weak ordering的答案。