Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using sort() facility of a list (std::list)

Status
Not open for further replies.

Deadlocker

Programmer
Jul 9, 2007
2
DE
Hi !

Here is what I want to do :

I have a class AElement that stores many information and particularly that implements a getX() function.
I have many AElements objects and I want to access them via some pointers. So I want to have a list of AElements because this data structures implements data structures and methods that are usefull for me.

Everything works fine until there, I can push, pop and print the list without any problem since the beginning of the afternoon (6 hours ago).

But I want to classify the elements regarding the returning value of getX(). SO I discovered that the list implements a "sort" facility we can use and "personalize" with our own comparison method. That's the point I'm facing so many troubles...


My code :

My comparison operator is as below :
template <class T>

struct sort_by_ptr
{
bool operator()(const T* left, const T* right)const
{
return ((left->getStartPos())->getX()) <= ((right->getStartPos())->getX());
}
};

and I call through :
list<AElement*> *aelist;
aelist = new list<AElement*>;
aelist->sort(sort_by_ptr<AElement>());

Thanks a lot for any comment ! :)
 
OK, so what's the problem you're having when you use your sort functor?
 
Your sort method should be the equivalent of a less than. That means that it should return true if the left side is less than the right side, false if the right side is less than the left side, and false if they are equal.

It looks like yours returns true if they are equal. Use < instead of <=.
 
Uups, I forgot to wrtie the code I get:

C:\Dokumente und Einstellungen\piouf.cpp(116) : error C2664: 'void __thiscall std::list<class AElement *,class std::allocator<class AElement *> >::sort(struct std::greater<class
AElement *>)' : cannot convert parameter 1 from 'struct SortEndPos' to 'struct std::greater<class AElement *>'
No constructor could take the source type, or constructor overload resolution was ambiguous

I tried only '<' but it doesnt work. I guess the problem doesnt come from my comparison instructions since we can put whatever we want...
So the problem comes from the syntx / declaration of my comparison function and maybe from the way I try to call it...
 
Try deriving your comparison functor from std::greater<AElement*>

of if that doesn't help, try creating the functor first, then pass it to sort() like this:
Code:
sort_by_ptr<AElement> functor;
aelist->sort( functor );

Sometimes using code like this:
Code:
sort_by_ptr<AElement>[b]()[/b]
will declare a function instead of creating an object, but I'm too hot and too tired to remember exactly which cases when that is true.

Also, why are you using a pointer to a list of pointers?
If the list must hold pointers instead of objects, you can make things easier on yourself by at least making the list itself an object instead of a pointer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top