struct vector_nodes_less
{
inline bool operator()(const Vector2i& a, const Vector2i& b) const
{
if (a.x == b.x && a.y == b.y) return false;
if (a.x < b.x && a.y < b.y) return true;
if (a.x > b.x && a.y > b.y) return false;
return (a.x < b.x || a.y < b.y);
}
};
typedef std::map <Vector2i, node*, vector_nodes_less> vector_nodes;
当我使用上述类型进行操作时,它会抛出无效的运算符<。
当键 (0,0) (0,1) 和我对 (1,0) vector 键执行操作时会发生这种情况。
我知道我需要弱严格排序,但我的功能应该没问题吧?
非常欢迎任何帮助,因为这会减慢我的速度。
问候
山姆
最佳答案
不允许同时拥有a < b
和 b < a
因此,如果您的运算符为一对元素返回 true,它应该以相反的顺序为它们返回 false。现在考虑对 (0, 1) 和 (1, 0)
编写运算符的最简单方法(如果您不关心特定顺序)是重用对的 operator<
return std::tie(a.x, a.y) < std::tie(b.x, b.y);
关于C++ 映射有时会抛出无效的运算符<,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32618261/