std::set with user defined type, how to ensure no duplicates


So I have an std::set which needs to keep specific ordering as well as not allowing duplicates of a user defined (by me) type. Now I can get the order to work correctly by overloading the '<' operator in my type. However, the set does not appropriately detect duplicates, and to be honest I'm not entirely sure how it does this internally. I have overloaded the '==' operator, but somehow im not sure this is what the set is actually using? So the question is how does the set determine duplicates when you add values? Here is the relevant code:

The user defined type:

//! An element used in the route calculation.
struct RouteElem {
    int shortestToHere; // Shortest distance from the start.
    int heuristic;      // The heuristic estimate to the goal.
    Coordinate position;
    bool operator<( const RouteElem& other ) const
        return (heuristic+shortestToHere) < (other.heuristic+other.shortestToHere);
    bool operator==( const RouteElem& other ) const
        return (position.x == other.position.x && position.y == other.position.y);

So the elements are equivalent when their position is equivalent, and an element is less than another if its combined functional is less than that of the other. The sorting works, but the set will accept two elements of the same position.

7/11/2009 10:50:07 PM

operator== is not used by std::set. Elements a and b are considered equal iff !(a < b) && !(b < a)

11/10/2015 7:19:29 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow