public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: help with STL needed
@ 2004-05-06 15:52 Martin York
  0 siblings, 0 replies; 4+ messages in thread
From: Martin York @ 2004-05-06 15:52 UTC (permalink / raw)
  To: gcc-help

 

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: help with STL needed
  2004-05-06 15:10 gnuml
  2004-05-06 15:31 ` Chris Wolstenholme
@ 2004-05-06 15:37 ` Eljay Love-Jensen
  1 sibling, 0 replies; 4+ messages in thread
From: Eljay Love-Jensen @ 2004-05-06 15:37 UTC (permalink / raw)
  To: gnuml, gcc-help

Hi Martin,

The key in your set is the POINTER, not the object.  The pointer is 
different between obj1 and obj3, so they are different objects.

Use a std::map, not a std::set.  For the key in your std::map, use the 
public key of the object you are sticking in the std::map.

Or make your classes monostate or singleton.  But that's not really what 
you'd want, I'd think.

--Eljay

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: help with STL needed
  2004-05-06 15:10 gnuml
@ 2004-05-06 15:31 ` Chris Wolstenholme
  2004-05-06 15:37 ` Eljay Love-Jensen
  1 sibling, 0 replies; 4+ messages in thread
From: Chris Wolstenholme @ 2004-05-06 15:31 UTC (permalink / raw)
  To: gnuml; +Cc: gcc-help

Hi,

The two objects in your code are both inserted in the set because the set's
key is a pointer to the class. obj1's pointer value is different to obj3's
pointer value, and so they can both go in the set (it's basically doing an
int's < operator, not the class')

As obj4 is never inserted, it can't be found as it is looking for a matching
pointer, not the class.

In the == statement you are using the * dereference, so now the class' ==
operator is being used, and obj1 is the same as obj3.

Hope this clears things up...

Chris




----- Original Message ----- 
From: "gnuml" <gnuml@bootweb.nl>
To: <gcc-help@gcc.gnu.org>
Sent: Thursday, May 06, 2004 4:09 PM
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);
> }
>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* help with STL needed
@ 2004-05-06 15:10 gnuml
  2004-05-06 15:31 ` Chris Wolstenholme
  2004-05-06 15:37 ` Eljay Love-Jensen
  0 siblings, 2 replies; 4+ messages in thread
From: gnuml @ 2004-05-06 15:10 UTC (permalink / raw)
  To: gcc-help

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-05-06 15:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-05-06 15:52 help with STL needed Martin York
  -- strict thread matches above, loose matches on Subject: below --
2004-05-06 15:10 gnuml
2004-05-06 15:31 ` Chris Wolstenholme
2004-05-06 15:37 ` Eljay Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).