STL set with object pointer list

A

Ami

Hi All,
I am trying to create a sorted list of class objects using set<
myclass* >. My class has one member variable which i used as
comparison criteria. I have over loaded the "<" operator also so that
objects should be inserted in sorted order but problem which i face is
that this overloaded function is not called at all. Please can anybody
help me to make it working.
code snippet is as follows:

//class
class myclass
{
public:
myclass(int data);
bool operator <(const myclass& otherclass) const;
int getdata(){return m_data;}
private:
int m_data;
}

//constructor
myclass::myclass(int data)
{
m_data=data;
}

//operator overloaded for comparision
bool myclass::eek:perator <(const myclass& otherclass) const
{
retrun (getdata() < otherclass.getdata());
}

//class set
set<myclass* > myclassset;

void main()
{
myclass *obj1 = new myclass(1);
myclass *obj2 = new myclass(2);

myclassset.insert(obj1);
myclassset.insert(obj2);


.......................
.....................
}

Thanks and Regards
 
O

Ondra Holub

Hi All,
I am trying to create a sorted list of class objects using set<
myclass* >. My class has one member variable which i used as
comparison criteria. I have over loaded the "<" operator also so that
objects should be inserted in sorted order but problem which i face is
that this overloaded function is not called at all. Please can anybody
help me to make it working.
code snippet is as follows:

//class
class myclass
{
public:
myclass(int data);
bool operator <(const myclass& otherclass) const;
int getdata(){return m_data;}
private:
int m_data;

}

//constructor
myclass::myclass(int data)
{
m_data=data;

}

//operator overloaded for comparision
bool myclass::eek:perator <(const myclass& otherclass) const
{
retrun (getdata() < otherclass.getdata());

}

//class set
set<myclass* > myclassset;

void main()
{
myclass *obj1 = new myclass(1);
myclass *obj2 = new myclass(2);

myclassset.insert(obj1);
myclassset.insert(obj2);

.......................
.....................

}

Thanks and Regards


You have to supply compare class as second parameter of set template.
because currently only pointers are compared (not instances, for which
you have operator<).

struct Compare
{
bool operator()(myclass* obj1, myclass* obj2) const
{
// Here write your own comparison
return *obj1 < *obj2;
}
};

std::set<myclass*, Compare> MySet;
 
A

Ami

You have to supply compare class as second parameter of set template.
because currently only pointers are compared (not instances, for which
you have operator<).

struct Compare
{
bool operator()(myclass* obj1, myclass* obj2) const
{
// Here write your own comparison
return *obj1 < *obj2;
}

};

std::set<myclass*, Compare> MySet;

Hi Ondra Holub,
Thats workkkk!!! Thanks a lot for your suggestion and help.
Many regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top