嗨,我定义了自定义键,如下所示。当我创建一个 std::map 时,我的印象是 map 将引用我的键中定义的 operator== 来检测两个键是否相同,但事实并非如此。您能否指出我从该地图中消除重复项的正确逻辑?
class Key
{
public:
Key(char * init, long l): equipNumber(l)
{
memcpy(initials, init, sizeof(initials));
}
bool operator==(const Key & other) const
{
bool result = true;
cout << "Comparing: " << initials << " with " << other.initials;
result &= (!memcmp(initials, other.initials, sizeof(initials)));
cout << " And result is: " << result << endl;
cout << "Comparing: " << equipNumber << " with " << other.equipNumber << endl;
result &= (equipNumber == other.equipNumber);
return result;
}
bool operator<(const Key & other) const
{
bool result = true;
result &= (equipNumber < other.equipNumber);
return result;
}
private:
char initials[5];
long equipNumber;
};