views:

17

answers:

1

Good Evening (depending on where u are right now). I am a little confused with the stl stuff for sorted sets... I want to store pointers of a custom class in my set and I want them to be sorted by my own criterion and not just the pointer size.

Anyone has an idea how to do this? Since it is impossible to do it like operator<(const foo &*rhs, const foo &*lhs){..};

Any suggestions? Thanks in advance and kind regards.

+2  A: 

std::set's second template parameter is the method it uses for comparisons. So you can do something like this:

struct dereference_compare
{
    template <typename T>
    bool operator()(const T* pX, const T* pY) const
    {
        return *pX < *pY;
    }
};

typedef std::set<T*, dereference_compare> set_type;
GMan