0

I have some classes, one for example is something like:

class foo {

    private:
        int number;
        ...

    public:

        foo(int set_number):number(set_number) {}
        ...

        bool operator<(const foo &myfoo) const
        { 
            return (number < myfoo.number ? 1 : 0); 
        }
        /* operator < */

        bool operator==(const foo &myfoo) const
        { 
             return (number == myfoo.number ? 1 : 0); 
        }
        /* operator == */

        ...
};

And I was using something similar to this class with set:

class hello {
    private:
        set<foo*> list;
        set<int>  int_list;
        ...
    public:
        ...
        fillList(void)
        {
           for(int i=0; i<10; i++)
           {
              list.insert(new foo(i));
              int_list.insert(i);
           }
        }
        ...
};

Ok, so when I print the result I get:

int_list: 0, 1, 2, 3, 4, ... 
list: 0, 6, 4, 5, ... (Using a method to print the attribute "number" from the object foo).

Obviusly the second is not ok. I guess it has to do with the fact that set is made of pointers, not of objects.

Now, should I use my set just like set< foo > ? Or is there a solution for this using pointers ?. Maybe I can overload the operators using something like:

bool operator<(const *foo myvar, const *foo myvar2); 

I was using this "set< foo* >". Because I was taught to use this way in vector< type > objects.

What's the best option ?

Ediolot
  • 459
  • 1
  • 5
  • 16

1 Answers1

1

If you want a custom sorting in set, you can't override < operator because:

set<foo*>

Its a set of pointers. Use instead:

set<foo*, compare>

And:

struct compare {
    bool operator() (const foo * lhs, const foo * rhs) const{
        return lhs->operator<(*rhs);
    }
};

Reason

Your set is sorting pointers. If you add compare to set<foo*, compare>, you force to use the < operator of the pointed class.

Alternative not using pointers

Just use set<foo> and remove new from list.insert(new foo(i));.

lilezek
  • 5,800
  • 19
  • 43
  • 1
    @πάνταῥεῖ He asked for it. Pointers are not needed, but it is a totally legal question. Someone may have the need to sort a set of pointers and this is how should be done. – lilezek Oct 17 '15 at 20:17
  • @Ediolot It may or may not, but if you use pointers you need to sort them this way. – lilezek Oct 17 '15 at 20:19
  • @lilezek Thanks, I was looking and found this exact question here, just in case someone needs it http://stackoverflow.com/questions/141337/c-stl-should-i-store-entire-objects-or-pointers-to-objects – Ediolot Oct 17 '15 at 20:24
  • I want to clarify something I still dont get. Why does that have to go inside an struct, and cant it be just somewhere in the class(without being inside a struct). And why do you overload the operator () instead of < or > ? – Ediolot Oct 17 '15 at 21:50
  • 1
    @Ediolot it is how does custom comparation work. You need a typename which is callable, so setting a structure and overriding () you have a typename which is callable. – lilezek Oct 18 '15 at 17:10