I am using std::map with key as a user-defined Struct and value as an int. Concretely, it looks something like this: map<myType, int>.
My Struct, let's call it myType, has 2 attributes, call them .a and .b, both of which are of type int.
When I do something like the following
mp[{5, 7}] = 0;
mp[{7, 5}] = 1;
and I access mp[{5, 7}], I get 1, instead of 0. This means that {.a, .b} and {.b, .a} map to the same value.
My understanding is that the root of the problem lies in the comparison operator I defined for myType.
Here is my definition of myType, with the comparison operator included:
struct myType {
int a;
int b;
bool operator<(myType m) const {
if (a == m.a) return (b <= m.b);
else return (a <= m.a);
}
};
How can I make {.a, .b} and {.b, .a} map to different values without using std::pair?
Aucun commentaire:
Enregistrer un commentaire