<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 3 年前。
<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 3 年前。
我正在尝试将结构用作映射中的键,但尽管定义了 <
和 ==
运营商我收到误报:
return models[mr];
即使新的 ImportSettings
也正在返回有reverse = true
模型中的那个有`reverse = false。
Visual Studio 调试器还显示运算符调用了 ==
甚至没有被调用。我在这里误解了什么?
struct HE2_ImportSettings
{
bool reverseWindingOrder = false;
bool operator==(const HE2_ImportSettings& other)const
{
return reverseWindingOrder == other.reverseWindingOrder;
}
};
struct ModelReference
{
std::string filepath = "";
HE2_ImportSettings importSettings = {};
bool ModelReference::operator == (const ModelReference& other) const
{
return filepath == other.filepath && importSettings == other.importSettings;
}
bool ModelReference::operator< (const ModelReference& other) const
{
return filepath < other.filepath;
}
};
ModelReference mr = { filename, importSettings };
bool exists = models.count(mr);
if (exists)
{
//This is returning even when the new importsettings has reverse = true and the one in models has reverse = false
return models[mr];
}
最佳答案
std::map
使用等效的概念。
!(a < b) && !(b < a)
换句话说, map 从不使用 ==
查看两个元素是否相同。在你的 map 中,当两个元素具有相同的 filepath
时,它们被认为是等价的。 .如果你也想带importSettings
考虑到 map 中的顺序,您还需要比较 operator<
中的顺序.
关于c++ - 使用 Structs 作为映射中的键,忽略 == 运算符,误报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59613045/