Implementing operator< for x,y,z coordinate

c++, stl

Solution

I assume you just want any stable order so an ordered container will work.

if ( X != v.X ) return X < v.X;
if ( Y != v.Y ) return Y < v.Y;
return Z < v.Z; 

What this does: you order based on X unless the X's are equal, if so you order on Y, etc.

Problem

I have this type which is basically a ``` struct { int x,y,z; } ``` that I want to use as a key for a stl map. Since it's a custom type, I need to implement the operator< for the map to do it's compare magic. I'm having a hard time coming with the function that will allow that. So far, I've tried : ``` return X < v.X && Y < v.Y && Z < v.Z; ``` which is not working at all, and ``` return X*X+Y*Y+Z*Z < v.X*v.X+v.Y*v.Y+v.Z*v.Z; ``` which gives this shape instead of a square: Keep in mind, the x,y or z value could be negative, which further invalidates the later solution. Anyone have any idea how to implement such feature?

Original source