Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 排序比较函数可以是成员函数上的指针吗?_C++_Sorting_Member Function Pointers - Fatal编程技术网

C++ 排序比较函数可以是成员函数上的指针吗?

C++ 排序比较函数可以是成员函数上的指针吗?,c++,sorting,member-function-pointers,C++,Sorting,Member Function Pointers,我想要一个按极角对点向量排序的函数,但我希望能够选择原点和方向(例如,为了使用Graham扫描计算凸包,需要按极角对点进行排序,相对于最底/最左点)。为了避免为原点和方向生成全局变量,我将它们隐藏在myComparisonClass类中。这是我的密码: template<typename TT> class myComparisonClass { point<TT> origin; point<TT> direction; public:

我想要一个按极角对点向量排序的函数,但我希望能够选择原点和方向(例如,为了使用Graham扫描计算凸包,需要按极角对点进行排序,相对于最底/最左点)。为了避免为原点和方向生成全局变量,我将它们隐藏在myComparisonClass类中。这是我的密码:

template<typename TT>
class myComparisonClass {
    point<TT> origin;
    point<TT> direction;
public:
    inline myComparisonClass (point<TT> or, point<TT> dir) : origin(or), direction(dir) {};

    bool myComparisonFunction(const point <TT>& a, const point<TT>& b) {  
        /* ... A function which use a, b, origin and direction ... */
        return false;
    };
};

template<typename TT>
void mySortByPolarAngle(vector<point<TT> >& P, point<TT> O, point<TT> dir) {
    myComparisonClass<TT> obj(O,dir);
    bool (myComparisonClass<TT>::* myFunctionPointer)(const point<TT>&, const point<TT>&) = &myComparisonClass<TT>::myComparisonFunction;
    sort(P.begin(), P.end(), obj.*myFunctionPointer); /* When trying to use mySortByPolarAngle, compiler says error: invalid use of non-static member function */
}
模板
类myComparisonClass{
点源;
点方向;
公众:
内联myComparisonClass(点或,点方向):原点(或),方向(方向){};
布尔myComparisonFunction(常数点&a,常数点&b){
/*…使用A、b、原点和方向的函数*/
返回false;
};
};
模板
无效mySortByPolarAngle(向量和P、点O、点方向){
myComparisonClass obj(O,dir);
bool(myComparisonClass::*myFunctionPointer)(常量点和,常量点和)=&myComparisonClass::myComparisonFunction;
排序(P.begin(),P.end(),obj.*myFunctionPointer);/*当尝试使用mySortByPolarAngle时,编译器说错误:非静态成员函数的使用无效*/
}
怎么了?是否可以将“sort”与比较函数(非静态成员函数上的指针)一起使用?
感谢

不可能将指向非静态成员函数的指针传递到
std::sort
:该函数需要能够在没有对象引用的情况下调用比较函数,而这是成员函数无法做到的

对于像您这样的情况,您需要传递一个函数对象,其中有一个运算符
()
用于比较两个对象:

template<typename TT>
class myComparisonClass {
    point<TT> origin;
    point<TT> direction;
public:
    inline myComparisonClass (point<TT> or, point<TT> dir) : origin(or), direction(dir) {};

    bool operator()(const point <TT>& a, const point<TT>& b) {  
        /* ... A function which use a, b, origin and direction ... */
        return false;
    };
};
...
template<typename TT>
void mySortByPolarAngle(vector<point<TT> >& P, point<TT> O, point<TT> dir) {
    sort(P.begin(), P.end(), myComparisonClass(O, dir));
}
模板
类myComparisonClass{
点源;
点方向;
公众:
内联myComparisonClass(点或,点方向):原点(或),方向(方向){};
布尔运算符()(常数点&a,常数点&b){
/*…使用A、b、原点和方向的函数*/
返回false;
};
};
...
模板
无效mySortByPolarAngle(向量和P、点O、点方向){
排序(P.begin(),P.end(),myComparisonClass(O,dir));
}

不可能将指向非静态成员函数的指针传递到
std::sort
:该函数需要能够在没有对象引用的情况下调用比较函数,这是成员函数无法做到的

对于像您这样的情况,您需要传递一个函数对象,其中有一个运算符
()
用于比较两个对象:

template<typename TT>
class myComparisonClass {
    point<TT> origin;
    point<TT> direction;
public:
    inline myComparisonClass (point<TT> or, point<TT> dir) : origin(or), direction(dir) {};

    bool operator()(const point <TT>& a, const point<TT>& b) {  
        /* ... A function which use a, b, origin and direction ... */
        return false;
    };
};
...
template<typename TT>
void mySortByPolarAngle(vector<point<TT> >& P, point<TT> O, point<TT> dir) {
    sort(P.begin(), P.end(), myComparisonClass(O, dir));
}
模板
类myComparisonClass{
点源;
点方向;
公众:
内联myComparisonClass(点或,点方向):原点(或),方向(方向){};
布尔运算符()(常数点&a,常数点&b){
/*…使用A、b、原点和方向的函数*/
返回false;
};
};
...
模板
无效mySortByPolarAngle(向量和P、点O、点方向){
排序(P.begin(),P.end(),myComparisonClass(O,dir));
}

非静态成员函数需要调用对象

在您的情况下,可以使用lambda函数,如下所示:

std::sort(P.begin(), P.end(),
          [&obj](const point<TT>& lhs, const point<TT>& rhs){
              return obj.myComparisonFunction(lhs, rhs);
          });
排序(P.begin(),P.end(), [&obj](常量点和左侧、常量点和右侧){ 返回对象比较函数(lhs、rhs); });
或者将类重命名为
myComparisonFunction
operator()
,并直接传递
obj
functor(
std::sort(p.begin(),p.end(),obj)
)。

非静态成员函数需要调用对象

在您的情况下,可以使用lambda函数,如下所示:

std::sort(P.begin(), P.end(),
          [&obj](const point<TT>& lhs, const point<TT>& rhs){
              return obj.myComparisonFunction(lhs, rhs);
          });
排序(P.begin(),P.end(), [&obj](常量点和左侧、常量点和右侧){ 返回对象比较函数(lhs、rhs); }); 或者将类重命名为
myComparisonFunction
operator()
,并直接传递
obj
functor(
std::sort(p.begin(),p.end(),obj)