Sorting a vector with three-dimensional points by coordinate value - syntax

I want to sort the point_vec vector as shown below in pseudo code. I want to sort this vector by coordinate value, for example x or y or z

class A{

  std:vector<double*> points_vec;

  void doSomething();

}

Then in the method A::doSomething,I want to sort this vector:

void A::doSomething() {
    std::sort(points_vec.begin(), points_vec.end(), sortPoints());
}

Can someone please show me the syntax of the method sortPoints().. Preferably, I want it to be a method of class A. This post creates structto do this, not sure if I have to create a similar one structinside the class. Is there any other way to handle this?

thank

+5
source share
5 answers

: / sort, operator< . A . , .

 struct Point {
     double x_, y_, z_;
     Point(double x, double y, double z) : x_(x), y_(y), z_(z) {}              
     // just an example, you can refine the following as much as you need
     bool operator<(Point const& other) {
         return x < other.x;
     }
 };  

 bool sortOnY(Point const& l, Point const& r) const {
      return l.y < r.y;
 }

 class A {
     std::vector<Point> pts_;
     void doSomething() {
          sort(pts_.begin(), pts_.end());
     }
     // if sorting on y is also required, you will need 
     // to use a custom comparator which can be either 
     // a functor or a function
     void doSomeOtherThing() {
         sort(pts_.begin(), pts_.end(), sortOnY);
     }
 };         
+5

- , sort . :

struct Compare
{
  bool operator()(double* first, double* second) const
 {
   //Compare points here
 }
};

:

std::sort(p.begin(), p.end(), Compare());

EDIT OP: , :

class A
{
public:
    struct c
    {
        bool operator()(int a, int b) const
        {
            return a < b;
        }
    };
};
int main()
{
    std::vector<int> a1;
    a1.push_back(2);
    a1.push_back(1);
    std::sort(a1.begin(), a1.end(), A::c());

    return 0;
}
+5

- , , - ", 3 ".

:

  • , Point3D
  • Point3D
  • std:: sort (points_vec.begin(), points_vec.end());

-, sort () .

+2

, Boost.Bind:

struct Point3D {
    double x, y;
    Point3D(double x=0., double y=0.) : x(x), y(y) {
    }
};

int main() {

    std::vector<Point3D> points;
    points.push_back(Point3D(-1.,  2.));
    points.push_back(Point3D( 2., -1.));
    points.push_back(Point3D(-2.,  0.));

    using boost::bind;
    std::sort(points.begin(), points.end(), 
              bind(&Point3D::x, _1) < bind(&Point3D::x, _2));
    // points sorted by x coord

    std::sort(points.begin(), points.end(), 
              bind(&Point3D::y, _1) < bind(&Point3D::y, _2));
    // points sorted by y coord
}

std::tr1::bind . , , ++ 0x :

std::sort(points.begin(), points.end(), 
          [](Point3D const & a, Point3D const & b) { return a.x < b.x; });
+2

x y z, . - , std::sort. , .

struct coord_comparison {
    int coord_id; // <= critical information
    bool operator()( double (*l)[3], double (*r)[3] ) {
        return (*l)[ coord_id ] < (*r)[ coord_id ];
    }
    coord_comparison( int id ) { coord_id = id; }
};

, , , operator() . :

std::sort(points_vec.begin(), points_vec.end(), compare_points( 1 /*for y*/) );

3 :

std:vector<double*> points_vec;

, double* . :

std:vector<double(*)[3]> points_vec;

std::sort - , :

bool compare_coords( double(*l)[3], double(*r)[3] ) {

, std::less:

    return std::less( *l, *l + ( sizeof *l/sizeof **l ), r );

(, , , )

    return std::less( *l, *l + 3, r );
}

This function can be useful outside the class, so I would make it free. You must do it staticif it stays inside the class.

Finally, leave parens without parameters when passing the function std::sort:

std::sort(points_vec.begin(), points_vec.end(), compare_points );
+1
source

All Articles