STL设置自定义排序

Com*_*erd 0 c++ sorting debugging stl set

当我意识到不可能使用一套装置而我必须创建一个新装置并拥有一个自定义排序功能来诉诸它时,我试图求助于一套.我在网上研究并试图实现我自己的自定义排序功能,但我不知道如何去做

这是我的班级

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;


};
Run Code Online (Sandbox Code Playgroud)

目前它是根据x值后跟y值排序,我想根据它来求助它

y值后跟x值

因此我实现了这种自定义排序

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;
}
Run Code Online (Sandbox Code Playgroud)

这是我如何尝试使用该集合的示例代码,

#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;  ;
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮我谢谢吗?

jua*_*nza 5

这将是一种更简单的方法:

#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);
  }
};
Run Code Online (Sandbox Code Playgroud)

然后

set<Point2D, SortByYX> p2d_set2(p2d_set.begin(), p2d_set.end());
Run Code Online (Sandbox Code Playgroud)

编辑:std::tie需要C++ 11支持,但如果你没有它,你可以使用std::tr1::tiefrom <tr1/tuple>,或者boost::tie如果你没有TR1.