English 中文(简体)
函子排序:向量
原标题:
  • 时间:2009-03-25 09:44:24
  •  标签:

我有这门课。

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

问题回答

你的order函子是正确的方式;只是你好像忘记初始化它的o成员。

你试过吗?

class order{
public:
  QSweep* o;
  order(QSweep* o_) : o(o_) {}
  bool operator() (const vector<int>& edge1, const vector<int>& edge2){
    return o->myPoints_[edge1[0]][0]<o->myPoints_[edge2[0]][0];
  }
};

sort(myVector1.begin(),myVector1.end(),order(qsweep));

比较函数是可以的,sort 不需要所有元素都与其他元素相比较大小。

乍一看,从您的文本中,似乎 static bool QSweep :: sortx(...)无效地使用了 this 数据成员。 这解释了编译器错误。 请注意,在您提供的源代码中,该函数不是静态的。

乍一看,您无法将不完整的排序定义为std::sort:如果您说“我不在乎我的第二个成员是大还是小,如果第一个成员更大,则整个对象更大”,则会使sort算法对具有相等的第一个成员的值处于空中。

这就是为什么 sort 函数需要一个 严格弱序 。你必须为具有相同第一个成员的值定义顺序。

sort(myVector1.begin(), myVector1.end(), sortx); 使用中文时与英文一致,不需要做任何修改。

你需要告诉 std::sort,你正在 main 中使用它,如果你想使用成员函数 sortx,就要使用这个形式。尝试:

sort(myVector1.begin(),myVector1.end(), std::mem_fun_ref(&QSweep::sortx));

另外,您的排序函数参数不正确:

布尔类型 sortx(const vector& edge1, const vector& edge2)

为了对您的矢量进行排序,您的sortx应该采取:

bool sortx(const vector<int>& edge1, const vector<int>& edge2)

另外,请注意,与多维数组一样,您无法在向量的向量中使用多个索引。我认为您过于复杂化了问题 :)

这里有一种简化的解决方案,可以对元组Point类型进行排序:

#include <iostream>

#include <iterator>
#include <algorithm>
#include <vector>

using namespace std;

template <class _MyType>
struct Point  {
    Point() {}

    void insert(_MyType const& p) { 
        _pts.push_back(p); 
    }

    bool operator<(Point<_MyType> const& o) {
        return _pts[ 0 ] < o._pts[ 0 ];
    }

private:
    typename vector<_MyType> _pts;
};

int main()
{
    Point<int> p;
    p.insert(1); p.insert(0); p.insert(2); // {1, 0, 2}
    Point<int> q;
    q.insert(-1); q.insert(10); q.insert(2); // {-1, 10, 2}
    vector<Point<int> > sweep;
    sweep.push_back(p);
    sweep.push_back(q);
    sort(sweep.begin(), sweep.end());    

    return 0;
}




相关问题
热门标签