延伸.
我有:
struct Coord { int row, col ; bool operator<( const Coord& other ) const { return row < other.row && col < other.col ; } } ;
我想创建一个map<Coord, Node*>
,在那里你可以查找Node*
的Coord
.
问题是,它有错误.对the map<Coord, Node*>
的查找Coord
返回错误的.
我很难搞清楚这是否合适.
维基百科说,map [keys]需要严格的弱序.我做错了吗?有没有办法使它工作,或者地图的键是否可以"严格排序"的简单值?
基本上问题是自定义struct
作为我的std :: map的关键字需要什么?
我尝试编写一个std :: map <Vector3D,double>,其中colinear(并行或反并行)向量应该共享相同的密钥.
作为比较函数,我使用以下函数(在isEqualEnough()中具有1e-9容差),这是我在std :: map中使用(数学)向量创建的
struct Vector3DComparator
{
bool operator() (const Vector3D& lhsIn, const Vector3D& rhsIn) const
{
Vector3D lhs = lhsIn.absolute(); // make all members positive
Vector3D rhs = rhsIn.absolute();
if ((lhs.z < rhs.z))
return true;
if ((isEqualEnough(lhs.z, rhs.z))
&& (lhs.y < rhs.y))
return true;
if ((isEqualEnough(lhs.z, rhs.z))
&& (isEqualEnough(lhs.y, rhs.y))
&& (lhs.x < rhs.x))
return true;
return false;
}
};
Run Code Online (Sandbox Code Playgroud)
当我将一个立方体的法线插入我的地图时,我应该得到3个不同的值(因为我不关心方向)但我得到4: