help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

help with STL


From: Martin
Subject: help with STL
Date: Thu, 6 May 2004 16:53:45 +0200

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);
}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]