Why won't std::list sort?
Hi all,
I can't compile program in which I want to sort std::list:
Code:
void CGeomUtils::sortByRelativePolarAngle(const myPoint & relativeTo, std::list<myPoint> & aList)
{
CGeomUtils::relativePoint = relativeTo;
std::sort(aList.begin(), aList.end(), CGeomUtils::sortByRelativePolarAngle_callback);
}
Code:
bool CGeomUtils::sortByRelativePolarAngle_callback(myPoint a, myPoint b)
{
double pa1 = getRelativePolarAngle(a, CGeomUtils::relativePoint);
double pa2 = getRelativePolarAngle(b, CGeomUtils::relativePoint);
if (IS_EQUIV(pa1, pa2))
return true;
if (pa1 < pa2)
return true;
return false;
}
It won't compile and I'm getting errors like this:
e:\program files\microsoft visual studio\vc98\include\algorithm(592) : error C2784: '_D __cdecl std::operator -(const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &,const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not deduce temp
late argument for 'const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &' from 'class std::list<struct _myPoint,class std::allocator<struct _myPoint> >::iterator'
Can someone see where is error?
Thank you.
Re: Why won't std::list sort?
You should use std::list::sort, and not std::sort on std::list.
Reason being that std::sort needs Random Access Iterators - something std::list does not offer. Hence for sorting's sake, std::list offers it's own member sort method.
Re: Why won't std::list sort?
1) std::sort requires random access iterators. std::list has
bi-directional iterators.
2) you should use the sort() member function for std::list
3) If you are using vc++6 , the predicated version incorrectly
requires the predicate to be in a special form (I don't recall
the exact form (something about dreived from std::greater).
If possible, define operator < for MyPoint, and use the
non-predicated version of the std::list::sort member function.
Re: Why won't std::list sort?
Thank you both guys!
Unfortunately defining operator< for myPoint wouldn't be that nice since it is just representing a point in the coordinate system (x, y) and it depends on whether we want to sort the points according to y or x-coordinate. Yes, it would be possible setting some flag, but...I switched to std::vector after all.
Thank you for the info, it's very useful yet not very frequently noticed (especially the note about VC++ 6.0 compiler)
Re: Why won't std::list sort?
Quote:
Originally Posted by s_k
Unfortunately defining operator< for myPoint wouldn't be that nice since it is just representing a point in the coordinate system (x, y) and it depends on whether we want to sort the points according to y or x-coordinate. Yes, it would be possible setting some flag,
You don't need a flag or overload operator >, all you need are two separate sorting criteria. Then you call the 3 argument version of std::list::sort() that specifies which criteria to use for the sort.
Overloading operator < is just one way to do this. The other way is to specify the sorting criteria using a function object or a function pointer.
Regards,
Paul McKenzie