Question
This is a followup of my previous question in which I wanted to know the most efficient way of storing non-duplicate arbitrary data in an std::set.
The answers helpfully pointed out that for custom classes, you'd need to implement operator< (if I understood correctly).
However my particular data type is a vector (in the mathematical sense). What I want to do is store a new vector in the set only if it doesn't already exist. To that end, how can I implement the less-than operator between two vectors? It does not seem to make sense when there are 3 orthogonal dimensions to compare. What strategies can I use to do this?
Solution
std::set can accept any comparison class that implements strict weak comparison as its second template parameter, it doesn't have to be the '<' operator. Just write a comparison that works as '<' would:
struct Compare {
bool operator( )( const T& left, const T& right ) const {
if ( left.dimension1 < right.dimension1 ) return true;
if ( left.dimension2 < right.dimension2 ) return true;
return left.dimension3 < right.dimension3;
}
};