I'm having quite a hard time trying to debug my little piece of code:
std::map<glm::ivec3,int> myMap;
glm::ivec3 myVec(3, 3, 3);
myMap.find(myVec);
I get the following error:
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_function.h|237|error: no match for 'operator<' in '__x < __y'
Does that mean I can't check whether a glm::ivec3
is smaller than another?
I think that because a stl::map
is ordered, the compiler wants to check which pair comes first. I tried to make the key a pointer and it worked.
Isn't there a way to keep the key a value instead of a pointer? This makes me ask another question: how can compare with a greater than operation something that cannot be compared or that is slow to be compared?
Thank you! :)
You can implement a comparison function:
bool operator<(const glm::ivec& lhs, const glm::ivec& rhs)
{
return lhs.x < rhs.x ||
lhs.x == rhs.x && (lhs.y < rhs.y || lhs.y == rhs.y && lhs.z < rhs.z);
}
(change .x
, .y
, .z
to [0]
, [1]
, [2]
/ .first()
, .second()
, .third()
etc as necessary.
how can compare with a greater than operation something that cannot be compared or that is slow to be compared?
Your pointer hack isn't uncommon but isn't always useful and has to be done with care - specifically, if someone comes along to search in the map and wants to find an existing element, they need a pointer to the same existing object that was earlier stored in the map. Or, choose some arbitrary ordering even if it makes no particular sense in the real world - as long as it's consistent.
If a comparison is just slow, you can potentially do things like compare a hash value first then fall back on the slower comparison for rare collisions (or if your hash is long/strong enough, return false
on the assumption they're equal).