C++映射有时会抛出无效的运算符<

use*_*060 1 c++ visual-studio-2013

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;
Run Code Online (Sandbox Code Playgroud)

当我使用上述类型进行操作时,它会抛出无效的运算符<.

当键(0,0)(0,1)和I对(1,0)向量键执行操作时,会发生这种情况.

我明白我需要弱的严格排序,但我的功能应该是好的吗?

任何帮助都会受到欢迎,因为这会减慢我的速度.

问候

山姆

Ria*_*iaD 6

它不允许同时具有这两个a < b并且b < a因此如果运算符对于元素对返回true,则它应该以反转的顺序为它们返回false.现在考虑对(0,1)和(1,0)

编写运算符的最简单方法(如果您不关心特定顺序)是重用对 operator<

return std::tie(a.x, a.y) < std::tie(b.x, b.y);
Run Code Online (Sandbox Code Playgroud)