You need to provide a comparator. You do not want to implement operator< , and I agree with this solution. You should not provide meaningless functions for your class to satisfy the limitations of any container. Fortunately, you do not need operator< . But you need a function with behavior similar to operator< . This does not mean that one object is considered smaller than another. It just needs to ensure a strictly weak order. You can give him any name you want. For instance:
bool Compare_by_x_then_y_then_z(const Object3D& lhs, const Object3D& rhs) { if (lhs.getX() != rhs.getX()) return lhs.getX() < rhs.getX(); if (lhs.getY() != rhs.getY()) return lhs.getY() < rhs.getY(); return lhs.getZ() < rhs.getZ(); }
Then you provide this function to your set constructor:
typedef bool(*compT)(const Object3D&, const Object3D&); std::set<Object3D,compT> objects(Compare_by_x_then_y_then_z);
source share