如果 Y 值为 1,即使 X 值为 0,sf::Vector2i as key in std::map 显示为 (1, 1)

sf::Vector2i as key in std::map shows as (1, 1) if the Y value is 1 even if the X value is 0

我试图在我的程序中使用 sf::Vector2i 作为图块地图的键。正如我在标题中所写,无论 X 值是多少,它只会显示地图中 X 和 Y 的 Y 值。

这是调试器中的一张图片,说明了我的意思:

如您所见,键值如下所示:[0][0]、[1][1]、[2][2]、[3][3] 等,而实际值是我所期望的(0,0 - 0,1 - 0,2 - 0,3 等)。

地图声明:

std::map<sf::Vector2i, std::pair<sf::Vector2f, std::list<unsigned int>>, VectorComparator> tileMap;

key (sf::Vector2i) 是瓦片位置,值对中的 sf::Vector2f 是实际位置(因此 tileMap[0,1].first 将是 0,32 作为瓷砖间隔 32)

这是填充地图的循环:

int xPos {0}, yPos {0};
for (int i {0}; i < width / WorldInfo::tileSize; ++i)
{
    for (int z {0}; z < height / WorldInfo::tileSize; ++z)
    {
        tileMap[sf::Vector2i(i, z)].first = sf::Vector2f(xPos, yPos);
        tileMap[sf::Vector2i(i, z)].second = std::list<unsigned int>();

        yPos += WorldInfo::tileSize;
    }

    yPos = 0;
    xPos += WorldInfo::tileSize;
}

地图比较器:

struct VectorComparator
{
    bool operator() (sf::Vector2i lhs, sf::Vector2i rhs) const
    {
        return lhs.x + lhs.y < rhs.x + rhs.y;
    }
};

知道这里发生了什么吗?我不明白为什么值是正确的而键却没有反映出来。

编辑:

如果我像这样手动添加磁贴:

tileMap[sf::Vector2i(0, 5)].first = sf::Vector2f(50, 50);

如果先添加,该条目的键也将为 [0][0]。如果我一个接一个地添加它,它将是 [1][1] 等等。似乎键值不关心 sf::Vector2i?

的内容

你的比较运算符是错误的。

你比较两点的曼哈顿距离,这是错误的。 0,1点和1,0点的距离相同(0 + 1 == 1 + 0),因此tileMap中只会存在一个(后面一个)。

试试这个:

struct VectorComparator
{
    bool operator() (sf::Vector2i lhs, sf::Vector2i rhs) const
    {
        return lhs.x < rhs.x || (lhs.x == rhs.x && lhs.y < rhs.y);
    }
};

编辑: 作为一个次要的建议,我会使用 std::vector 而不是 std::list (几乎在所有情况下都更快,除非你真的需要指向始终有效的元素的指针)。我也会使用 std::unordered_map 而不是 std::map (再一次,更快)。