help with STL needed

Martin York martin.york@veritas.com
Thu May 6 15:52:00 GMT 2004


 

That's because your set is a set of pointers. The set is comparing the
pointers not their content.
The default comparison used by set is 'less<key>', but this can be
overridden.

use:
        typedef set<someclass*,someclassptest>  someclasspset;
        someclasspset           list;
        someclasspset::iterator it;

And then define the class 'someclassptest'



class someclassptest
{
    public: bool operator()(const someclass* lhs,const someclass* rhs)
    {
        return(lhs->key < rhs->key);  /* Note the use of the '<' rather
than '==' */
    }
};


-----Original Message-----
From: gnuml [mailto:gnuml@bootweb.nl] 
Sent: 06 May 2004 11:10
To: gcc-help@gcc.gnu.org
Subject: help with STL needed

Hi,

I am trying to use the STL-'set' container, but have a few problems with
it.

I have defined a certain class 'someclass' which is abstract. In that
class I have defined the <, > and == operators for it. Whenever 2
classes derived from 'someclass' have the same key, I want them to be
considered equal.
'class101' and 'class545' are derived from 'someclass'.
In main() I want pointers to objects of type 'someclass' to be stored in
a 'set', which means that no 2 elements of the set can be equal (in my
case:
no two objects that pointers point to, can have the same key). However,
when I try inserting 2 objects of type class545, the set successfully
does so, when in fact it should NOT because only one class with key
'545' is allowed.
Also, looking for an object of class101 (obj4 in my example) fails,
which shouldn't, because obj2 of class101 actually is in the set.
Finally, 'manually' comparing obj1 to obj3 works out just fine, which
leaves me wondering why the set-insert/find functions don't use the
operators I defined in someclass. Am I missing something here?

Thanks alot for any help!

Martin

#include<string>
#include<iostream>
#include<set>

using namespace std;

class someclass
{
public:
        string key;
        string data;

        virtual void dosomething() = 0;
        virtual ~someclass() = 0;

        bool operator<(someclass &rhs)
        {
                return key < rhs.key;
        }

        bool operator==(someclass &rhs)
        {
                return key == rhs.key;
        }

        bool operator>(someclass &rhs)
        {
                return key > rhs.key;
        }
};

someclass::~someclass()
{
}

class class101 : public someclass
{
public:
        class101( string d)
        {
                key = "101";
                data = d;
        }

        void dosomething()
        {
                cout << key << " : " << data << endl;
        }
};

class class545 : public someclass
{
public:
        class545( string d)
        {
                key = "545";
                data = d;
        }

        void dosomething()
        {
                cout << key << " => " << data << endl;
        }
};

int main()
{
        set<someclass*> list;
        set<someclass*>::iterator it;

        someclass *obj1 = new class545("dfdf");
        someclass *obj2 = new class101("fdgfdhgfhgf");
        someclass *obj3 = new class545("ds");

        someclass *obj4 = new class101("gdfgfd");

        list.insert(obj1);
        list.insert(obj2);
        list.insert(obj3);

        for(it = list.begin(); it != list.end(); it++)
        {
                (*it)->dosomething();
        }

        it = list.find(obj4);
        if( it == list.end() )
        {
                cout << "NOT FOUND!" << endl;
        }else{
                cout << "FOUND!" << endl;
        }


        if( (*obj1) == (*obj3) )
        {
                cout << "obj1 and obj3 are equal" << endl;
        }

        return(0);
}




More information about the Gcc-help mailing list