如何在需要成员数据时对std:list进行排序? [英] How to sort a std:list when you need member data?
问题描述
所以我会知道如何排序,如果我可以使用一个向量,但我们需要实现它使用列表。我们的教授说我们可以使用已经在列表类中实现的排序函数。如果它是一个向量,我可以创建一个结构,然后使用sort从&算法>去我的列表,但它不会让我使用,因为std :: list没有随机访问。 API说template< class Compare>但我不认为这会帮助我。
我知道我可以使用排序功能,但我需要使用成员数据进行排序。我要通过他们的极角对点进行排序,我需要使用作为我的类的成员的当前点作为原点,所以我不能像我通常会使用静态排序比较器。
$ b我用这个作为我的排序调用: sortedList.sort(sorting);
这里是我的函数:
bool排序(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;
}
}
我得到
c:\users\wooly\documents\visual studio 2010\projects\proj5\proj5\grahamscan.cpp(20):error C3867 :'GrahamScan :: sorting':函数调用缺少参数列表;使用'& GrahamScan :: sorting'来创建指向成员的指针
,因为我需要pointStack的顶值来做排序但它是我的类的成员。
如果你的值类型有operator&
std :: list< int> myList;
//对列表做事情
myList.sort();
或者如果不需要提供一个函数来做比较。 >
struct MyClassComparator {
bool operator()(const MyClass& first,const MyClass& second)const {
/ /返回true如果第一个应该在第二个之前
return true;
}
};
std :: list< MyClass> myList;
//对列表做事情
myList.sort(MyClassComparator());
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.
std::list<int> myList;
// Do stuff to the list
myList.sort();
Or if it doesn't you need to provide a functor that will do the comparison.
struct MyClassComparator {
bool operator()(const MyClass& first, const MyClass& second) const {
// Return true if first should go before second
return true;
}
};
std::list<MyClass> myList;
// Do stuff to the list
myList.sort(MyClassComparator());
这篇关于如何在需要成员数据时对std:list进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!