STL set custom sort

2019-03-04 09:25发布

问题:

I was trying to resort a set when i realised that it was impossible to resort a set and i had to create a new set and have a custom sort function to resort it . I researched online and tried to implement my own custom sort function but am not sure how to go about it

This is my class

class Point2D
{
 public:

           int getX() const;
           int getY() const;

           void setX(int);
           void setY(int);


          bool operator < ( const Point2D& x2) const
          {
            if ( x != x2.x)
            {
            return x < x2.x;
            }
            if ( y != x2.y)
            {
              return y < x2.y;
            }
          };

 protected:

             int x;
             int y;


};

Currently it is sorted according to x values followed by y values , I want to resort it according to

y values followed by x values

hence i implemented this custom sort

bool p2d_sortby_y(Point2D& ptd1 , Point2D& ptd2) //custom sort function
{
    if ( ptd1.getY() != ptd2.getY())
    {
        return ptd1.getY() < ptd2.getY();
    }
  if ( ptd1.getX() != ptd2.getX() )
    {
        return ptd1.getX() < ptd2.getX();
    }

    return false;
}

This is my sample code of how i am trying to resort the set ,

#include <iostream>
#include <string>
#include <fstream>
#include <set>
#include <cmath>
using namespace std;
class Point2D
{
 public:

           int getX() const;
           int getY() const;

           void setX(int);
           void setY(int);


          bool operator < ( const Point2D& x2) const
          {
            if ( x != x2.x)
            {
            return x < x2.x;
            }
            if ( y != x2.y)
            {
              return y < x2.y;
            }
          };

 protected:

             int x;
             int y;


};

bool p2d_sortby_y(Point2D& ptd1 , Point2D& ptd2) //custom sort function
{
    if ( ptd1.getY() != ptd2.getY())
    {
        return ptd1.getY() < ptd2.getY();
    }
  if ( ptd1.getX() != ptd2.getX() )
    {
        return ptd1.getX() < ptd2.getX();
    }

    return false;
}



int main()
{
    set<Point2D> p2d_set;

    Point2D p2d;

    p2d.setX(1);
    p2d.setY(3);

    p2d_set.insert(p2d);

    p2d.setX(3);
    p2d.setY(2);

    p2d_set.insert(p2d);

    set<Point2D>::iterator p2 = p2d_set.begin();

   while ( p2 != p2d_set.end() )
   { 
     cout<<p2->getX()
         <<" "
         <<p2->getY()
         <<endl;
     p2++;
   }


   set<Point2D,p2d_sortby_y> p2d_set2 = p2d_set; // i am unsure of how to implement the custom sort function here





}



int Point2D::getX() const
{
   return x;
}

int Point2D::getY() const
{
   return y;
}
void Point2D::setX(int x1)
{
   x = x1;
}

void Point2D::setY(int y1)
{
 y = y1;  ;
}

Can someone help me out thanks ??

回答1:

This would be an easier way to do it:

#include <tuple>

struct SortByYX
{
  bool operator ()(const Point2D& lhs, const Point2D& rhs) const
  {
    return std::tie(lhs.y, lhs.x) < std::tie(rhs.y, rhs.x);
  }
};

Then

set<Point2D, SortByYX> p2d_set2(p2d_set.begin(), p2d_set.end());

Edit: std::tie requires C++11 support, but if you don't have it you can use std::tr1::tie from <tr1/tuple>, or boost::tie if you don't have TR1.



回答2:

In set<Point2D, p2d_sortby_y> the second parameter p2d_sortby_y does not name a type.

struct point_odered_by_y
{
    bool operator()(const Point2D& ptd1, const Point2D& ptd2) const
    {
        /* .. */
    }
};



typedef std::set<Point2D, point_odered_by_y> pointset;
pointset s(begin(p2d_set), end(p2d_set));


回答3:

It's usually better to define a functor class to specify the ordering, rather than a function; that way, you can specify it as a template parameter:

struct p2d_sortby_y {
    bool operator()(Point2D x, Point2D y) {return whatever;}
};

This has to take its arguments by value or const reference (not non-const reference, as yours does) to be used on the constant set members.

You can't directly initialise the new set from a different type of set, but you can initialise it from its range:

set<Point2D,p2d_sortby_y> p2d_set2(p2d_set.begin(), p2d_set.end());


回答4:

Your two sets have different types (the compare function is part of the type definition).

Since std::set does not provide a constructor or assignment operator for different compare functions, you have to either initialize the new set with an iterator pair to your first set or std::copy over the elements.

That is:

set<Point2D,p2d_sortby_y> p2d_set2 { std::begin(p2d_set), std::end(p2d_set) };

or

set<Point2D,p2d_sortby_y> p2d_set2;
std::copy(std::begin(p2d_set), std::end(p2d_set), std::inserter(p2d_set2));