STL set custom sort

前端 未结 4 596
遥遥无期
遥遥无期 2021-01-27 17:03

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 onl

4条回答
  •  感动是毒
    2021-01-27 17:19

    This would be an easier way to do it:

    #include 
    
    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 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 , or boost::tie if you don't have TR1.

提交回复
热议问题