C++ 如何在可以接收其他对象指定信息的类中定义sort()中使用的cmp函数?
我有一个名为C++ 如何在可以接收其他对象指定信息的类中定义sort()中使用的cmp函数?,c++,class,C++,Class,我有一个名为Polygon的类,基本结构如下: class Polygon { public: Polygon(int _n): n(_n) { p = new Point2D[n+1](); } int n; Point2D *p; Point2D center; void initCenterPoint() { double x = 0, y = 0; for (int i=0; i<n; ++i) {
Polygon
的类,基本结构如下:
class Polygon {
public:
Polygon(int _n): n(_n) { p = new Point2D[n+1](); }
int n;
Point2D *p;
Point2D center;
void initCenterPoint() {
double x = 0, y = 0;
for (int i=0; i<n; ++i) {
x += p[i].x;
y += p[i].y;
}
center = Point2D(x/n, y/n);
}
void sortVertices() {
std::sort(p,p+n,less);
}
/** Basically this is what I want to do.
The sortVertices() method needs the compare method below, while
the compare method need to access public member 'center' in the
object. How can I implement this?
*/
bool less(const Point2D &a, const Point2D &b) {
return a.x + b.x - center.x > 0;
}
}
类多边形{
公众:
多边形(int _n):n(_n){p=newpoint2d[n+1]();}
int n;
点2d*p;
点2D中心;
void initCenterPoint(){
双x=0,y=0;
对于(int i=0;i 0;
}
}
我想实现sortVertices()函数,但<强>比较函数 <需要访问成员<代码>中心< /代码> <强> >不同的对象。我不能把它放在类中,或者给它另一个参数,也不能根据我的要求和C++中的限制声明它为“代码>静态<代码> >代码>朋友>代码>。
那么我该如何实现这一点呢?谢谢
更新:添加了有关情况的详细信息
遗憾的是,我们没有使用C++11,因此无法应用lambda表达式。您可以使用函子,而不是less
。每个多边形
将包含函子的一个实例,函子将引用中心
:
class Polygon {
public:
int n;
Point2D *p;
Point2D center;
struct Less
{
Less(const Point2D& p) : point_(p) {}
const Point2D& point_;
bool operator()(const Point2D& lhs, const Point2D& rhs)
{
// implement comparison using point_
}
};
Less lessCmp_; // instantiate in constructor init list
void sortVertices() {
std::sort(p,p+n, lessCmp_);
}
}'
您可以使用函子而不是less
。每个多边形
将包含函子的一个实例,并且函子将引用中心
:
class Polygon {
public:
int n;
Point2D *p;
Point2D center;
struct Less
{
Less(const Point2D& p) : point_(p) {}
const Point2D& point_;
bool operator()(const Point2D& lhs, const Point2D& rhs)
{
// implement comparison using point_
}
};
Less lessCmp_; // instantiate in constructor init list
void sortVertices() {
std::sort(p,p+n, lessCmp_);
}
}'
Lambdas可以帮助解决您的问题。您本质上希望创建一个函数“less”,该函数以某种方式了解未作为参数传入的变量。如果您不了解代码的功能,请查看(尤其是“使用Lambdas捕获变量”部分)
//myLess is not a member function
bool myLess (Point2D x, Point2D y, Point2D center) {
//Your sorting code here
}
...
//inside your class definition
void sortVertices() {
auto less = [] (Point2D x, Point2D y) {return myLess(x,y,center);};
std::sort(p,p+n,less);
}
Lambdas可以帮助解决您的问题。您本质上希望创建一个函数“less”,该函数以某种方式了解未作为参数传入的变量。如果您不了解代码的功能,请查看(尤其是“使用Lambdas捕获变量”部分)
//myLess is not a member function
bool myLess (Point2D x, Point2D y, Point2D center) {
//Your sorting code here
}
...
//inside your class definition
void sortVertices() {
auto less = [] (Point2D x, Point2D y) {return myLess(x,y,center);};
std::sort(p,p+n,less);
}
如果你能发布一些代码来说明这个问题,那会有帮助的。你能给我们展示一下lessthan()是什么吗函数的外观可能与访问/权限等无关?您的less
函数不能用于对任何内容进行排序,因为如果您交换a
和b
,结果是相同的。如果您可以发布一些说明问题的代码,这会有所帮助。您是否可以向我们展示lessthan()函数可能看起来像,而不必担心访问/权限等?您的less
函数不能用于排序任何内容,因为如果您交换a
和b
,结果是相同的。myLess函数可以是多边形的静态方法,而不是全局函数。myLess函数可以是多边形的静态方法gon而不是全局函数。谢谢,但仍然对如何实现这一点感到困惑。请你更具体一点好吗?我已经更新了问题并添加了更详细的代码。@renfei我不能比这更具体了。你对哪个部分有问题?顺便说一下,你的less
函数坏了。你不能使用那个标准to sort.应该是构造函数中的const Point2D&p
。@juanchopanza是的less
函数实际上比这更复杂,它只是一个(坏的)演示了如何使用center
。现在我想我已经理解了这个想法:我们把东西分配给point\u
,而不是center
,因为函子可以访问point\u
。谢谢,但仍然对如何实现感到困惑。请你更具体一点好吗?我已经更新了问题并添加了更多细节代码有问题。@renfei我不能说得比这更具体了。你对哪一部分有问题?顺便说一下,你的less
函数坏了。你不能用那个标准来排序。在构造函数中应该是const Point2D&p
。@juanchopanza是的less
函数实际上比这更复杂,不是吗这只是center
如何使用的一个(糟糕的)说明。现在我想我已经理解了这个想法:我们把东西分配给point
,而不是center
,因为函子可以访问point
。