我有一个类 cl1:
class c1
{
long double * coords;
...
}
我也有第二类 cl2:
class cl2
{
vector<cl1*> cl1_vec;
unsigned int d;
...
}
我想根据 coords[d] 对 cl2 中的 cl1_vec 进行排序,使用向量的 sort 函数。所以我可以有类似的东西
sort(cl2_inst->cl1_vec.begin(),cl2_inst->cl1_vec.end(), ??? );
我尝试了类似的方法
但我无法解决这个问题。
感谢您提供的任何帮助。
我试过的代码:
class cl1 {
public:
long double* coords;
cl1(long double *, unsigned int);
cl1();
cl1(const cl1& orig);
virtual ~cl1();
};
class cl2 {
public:
unsigned int d;
vector<cl1*> cl1_vec;
//the srting functions
static bool compareMyDataPredicate(cl1* lhs, cl1* rhs)
{
return (lhs->coords[d] < rhs->coords[d]);
};
// declare the functor nested within MyData.
struct compareMyDataFunctor : public binary_function<my_point*, my_point*, bool>
{
bool operator()( cl1* lhs, cl1* rhs)
{
return (lhs->coords[d] < rhs->coords[d]);
}
};
...
...
}
然后在主要
std::sort(cl2_inst->cl1_vec.begin(),cl2_inst->cl1_vec.end(),cl2::compareMyDataPredicate() );