我需要在课堂上进行排序:
typedef bool (*fcomp)(int,int);
class mf : public QAbstractTableModel{Q_OBJECT
public:
//horisontalHeader struct
struct hs {
QString h;//column name
Qt::AlignmentFlag a;//align
fcomp s;//compare function for std::sort
};
//data
hs h[cc]={
{t("Function"),Qt::AlignCenter,cf},
{t("Errors"),Qt::AlignCenter,cer}
};
bool cf(int li,int lj){return
(QString::localeAwareCompare(tf->r[li].f_name,tf->r[lj].f_name)<0);}
bool cer(int li,int lj){return
((tf->r[li].f_er)<(tf->r[lj].f_er));}
void dosort(int column){
std::sort(
i.begin(),
i.begin()+rowCount(),
h[column].s);
}
如何声明比较函数将它们存储在数组中?
编译错误:
cannot convert 'bool (mf::*)(int, int)' to
'fcomp {aka bool (*)(int, int)}' in initialization
在我使用How to do one quick sort procedure for different structures之前的方法之前,我不喜欢它:
// compare func for column 0
struct fcmp0{bool operator()(mNrow * li,mNrow * lj){
return (QString::localeAwareCompare(*(li->f_name),*(lj->f_name))<0);
}} f0;
//compare func for column 1
struct fcmp1{bool operator()(mNrow * li,mNrow * lj){
return ((li->f_er)<(lj->f_er));
}} f1;
if (column==0) {std::sort(r.begin(),r.end(),f0);}
else if (column==1) {std::sort(r.begin(),r.end(),f1);}