So I would know how to sort it if I could use a vector but we are required to implement it using a list. Our professor said that we can use the sort function already implemented in the list class. If it was a vector I could create a struct and then use sort from < algorithm > to go though my list but it won’t let me use that since std::list doesn’t have random access. The API says “template< class Compare >” but I don’t think that will help me.
I understand that I could use the sort function but I need to use member data to sort it. I’m sorting Points by their polar angle and I need to use the current Point that is a member of my class as the “origin” so I can’t use a static sorting comparator like I normally would.
EDIT
I’m using this as my sorting call:
sortedList.sort(sorting);
and here is my function:
bool sorting(const Point& p, const Point& q) {
Point z = pointStack.top();
Point u = Point(p.getX() - z.getX(), p.getY() - z.getY());
Point v = Point(q.getX() - z.getX(), q.getY() - z.getY());
double r = u.polarAngle();
double s = v.polarAngle();
if (r < s) {
return true;
} else {
return false;
}
}
I keep getting
c:\users\wooly\documents\visual studio 2010\projects\proj5\proj5\grahamscan.cpp(20): error C3867: ‘GrahamScan::sorting’: function call missing argument list; use ‘&GrahamScan::sorting’ to create a pointer to member
since I need the top value of pointStack to do the sorting but its a member of my class.
You can just call the sort function directly if your value type has operator< defined.
Or if it doesn’t you need to provide a functor that will do the comparison.