CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2001
    Location
    Trutnov, Czech Republic
    Posts
    459

    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:perator -(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.
    The sun is the same in the relative way, but you're older
    Shorter of breath and one day closer to death


    - Roger Waters, 1973

  2. #2
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    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.

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    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.

  4. #4
    Join Date
    Jul 2001
    Location
    Trutnov, Czech Republic
    Posts
    459

    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)
    The sun is the same in the relative way, but you're older
    Shorter of breath and one day closer to death


    - Roger Waters, 1973

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured