我有这门课。
class QSweep{
public:
QSweep(const vector< vector<double> >& myPoints, const vector< vector<int> >&myEdges);
void intersection(vector< vector<double> >& myPoints, vector< vector<int> >& myEdges);
vector< vector<int> >* sortEdges(vector< vector<double> >& myPoints,
vector<vector<int>>& myEdges);
bool sortx(const vector< vector<int> >& edge1, const vector< vector <int> >& edge2) {
return edge1[0][0]< edge2[0][0];
};
private:
vector< vector<double> > myPoints_;
vector< vector<int> > myEdges_;
};
构造函数以这种方式创建所需的数据结构:
QSweep::QSweep(const vector< vector<double> >& myPoints, const vector< vector<int>>&
myEdges){
myPoints_=vector< vector<double> > (myPoints.size(), vector< double >(3,0));
for (unsigned int i=0;i<myPoints.size();i++){
myPoints_[i][0]=myPoints[i][0];
myPoints_[i][1]=myPoints[i][1];
myPoints_[i][2]=myPoints[i][2];
}
myEdges_=vector< vector<int> > (myEdges.size(), vector< int >(2,0));
for (unsigned int i=0;i<myEdges.size();i++){
myEdges_[i][0]=myEdges[i][0];
myEdges_[i][1]=myEdges[i][1];
}
}
giving E.g: myPoints={{1.0,1.0,2.0},{18.0,4.0,2.0},{5.0,1.0,2.0}}; myEdges={{0,1},{1,2},{0,2}};
I want to have a sort function for myEdges 2-dimensional vector depending on the first coordinate of myPoints given by myEdges[i][0].E.g after sorting myEdges would become myEdges={{0,1},{0,2},{1,2}} because myPoints[myEdges[1][0]][0]
我试过了。
bool sortx(const vector<int> & edge1,const vector<int> & edge2) {
return myPoints_[edge1[0][0]< myPoints_[edge2[0][0];
};
in the public member of QSweep with the call:
int main(){
...
sort(myVector1.begin(),myVector1.end(),sortx);
}
but I keep having the same error (even if I modified something): sort.cpp: In static member function static bool QSweep::sortx(const std::vector >&, const std::vector >&) : sort.cpp:47: error: invalid use of member QSweep::myPoints_ in static member function sort.cpp:25: error: from this location sort.cpp:47: error: invalid use of member QSweep::myPoints_ in static member function sort.cpp:25: error: from this location sort.cpp: In function int main() : sort.cpp:106: error: sortx was not declared in this scope
Then I tried to write a functor for my class (which I never worked with before): The functor is:
class order{
public:
QSweep* o;
//vector< vector<double> > myPoints_;
bool operator() (const vector<int>& edge1, const vector<int>& edge2){
return o->myPoints_[edge1[0]][0]<o->myPoints_[edge2[0]][0];
}
};
and I defined it outside the Class QSweep. And I call the functor in the main program as:
sort(myVector1.begin(),myVector1.end(),order());
The program compiles but I get an bus error message: ./sort 1 2 2 3 1 3 Bus error
oii so definitely my functor is not good.. should I initialize the object of class QSweep somehow. I do not understand how this functor will know to use exactly myPoints_ elements from QSweep class..
thank you in advance, madalina
qsweep is an object of the QSweep class the one initialized in the program no. I use in my main program:
....
QSweep* s= new QSweep(myVector, myVector1);
....
使用在类订单中的o初始化方式,该程序通过以下调用进行编译和运行:
sort(myVector1.begin(),myVector1.end(),order(s));
而这个排序仅仅针对于我想要进行扫描算法的第一个元素而言。
thank you very much for your help! (I was starting to be really confused), madalina