tags:

views:

169

answers:

5
A: 

sort(myVector1.begin(),myVector1.end(),sortx);

You need to tell std::sort, which you are using inside main, if you are going to use this form, that you want to use the member function sortx. Try:

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

Also, your sort function takes incorrect parameters:

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

To sort your vector of vector of ints your sortx should take:

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

Also, note that you cannot use multiple indexes with a vector of vector as you can with multidimensional arrays. I think you have over complicated your problem :)

Here is a simplified solution for sorting your tuple Point type:

#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;
}
dirkgently
A: 

At first sight, from your text, it seems that static bool QSweep::sortx(...) makes invalid use of this data members. This explains the compiler error. Note that in the source code you provided, the function isn't static.

At second sight, you cannot define an incomplete ordering to std::sort: if you say "I don't care if my second member is bigger or smaller, if the first one is bigger, the whole object is bigger", you leave the sort algorithm in the void for values that have equal first members.

That's why sort takes a strict weak ordering. You have to define the order for values with an equal first member, too.

xtofl
A: 

what do you mean you dont access vector of vector with indexes? But how can I access then the elements? or how I could initializa the vectors of vectors? with push_back?or?

the problem is that the function:

static bool sortx(const vector<int>& edge1,const vector<int> &  edge2)  {
 return edge1[0]< edge2[0];
  };

with the call: sort(myVector1.begin(),myVector1.end(),&QSweep::sortx);

it is working, but I want another criteria for sorting my edges:

  myPoints_[edge1[0]][0]< myPoints_[edge2[0]][0];

and this I do not know how to do as I use here a member myPoints_ of QSweep. This is why I tried the functor:

class order{
public:
QSweep* o;
bool operator() (const vector<int>& edge1, const vector<int>& 

    edge2){
 return o->myPoints_[edge1[0]]

          [0]<o->myPoints_[edge2[0]][0];
}
};

but definitely is wrong.. thank you in advance, madalina

madalina
+2  A: 

Your order functor is the right way do go; it just seems you have omitted to initialize its o member.

Have you tried:

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));

The comparison function is OK, sort doesn't require all elements to compare greater or less than others.

jpalecek
A: 

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);
....

Using so this initialization for o in the class order, the program compiles and runs with the call:

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

And the sorting is performed only in regard to the first components of myPoints_ (as I really needed to do) for this sweep algorithm.

thank you very much for your help! (I was starting to be really confused), madalina

madalina
If you find jpalecek's answer helpful, please accept his answer.
dirkgently