0

Suppose I have a set std::set<*int> and I want the elements to be sorted by the integer to which they point rather than the pointer type; is there some standard comp function I can use from the std library? If not, how would I declare such a set?

I'm guessing I would have to do define my own comparison function but how does that look in practice?

quant
  • 19,524
  • 28
  • 103
  • 199

1 Answers1

3

Using the solution from Sort a std::list<myclass*> with myclass::operator<(myclass &other) it goes like this:

template <typename T>
struct PComp
{
    bool operator ()(const T* a, const T* b) const
    {
       return *a < *b;
    }
};

std::set<int*, PComp<int> > my_set;
Community
  • 1
  • 1
John Zwinck
  • 223,042
  • 33
  • 293
  • 407