0

For example,

#include <set>
class myclass
{
public:
    int a;
};
int main()
{
    std::set<myclass*> arr;
    for(m=0;m<3; m++){
        myclass * p = new myclass;
        p->a=m;
        arr.insert(p);
    }
}

In the above code, arr is sorting the address rather than the value a. How to make arr's member rank according the value of a.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
John
  • 125
  • 1
  • 8
  • Why are you storing pointers in the `set` in the first place? This is normally not what you want to do. – NathanOliver Dec 21 '18 at 19:47
  • provide second template argument to set: `std::set` – Jarod42 Dec 21 '18 at 19:49
  • what is `public a;`? – Matthieu Brucher Dec 21 '18 at 19:51
  • @NathanOliver Probably the pointers are to fix the compiler error when trying to use `myclass` directly, because `myclass` isn't (yet) comparable. – Daniel H Dec 21 '18 at 19:56
  • 1
    If you stored `std::set` instead of pointers, observed the compiler errors, then you would get an error that may have been more helpful to you. Basically, it looks like you fell right into the [XY Problem](http://xyproblem.info/) trap. – PaulMcKenzie Dec 21 '18 at 19:56
  • @PaulMcKenzie Maybe, but it could also be the inexplicable love that newbies have for pointers. – john Dec 21 '18 at 20:01

0 Answers0