C++ Sorting objects based on two data members

前端 未结 1 1635
遇见更好的自我
遇见更好的自我 2020-12-19 11:17

I understand you can insert a user-defined class in to a std::vector and then overload the sorting mechanism so that it compares on a particular data member. Ho

相关标签:
1条回答
  • 2020-12-19 12:12

    Create a custom comparator using std::tuple

        #include <tuple>
       //..    
        struct comp
        {
          bool operator()(const MyClass& lhs, const MyClass& rhs) const
          {
            return std::tie(lhs.a, lhs.b) < std::tie(rhs.a, rhs.b);
          }
        };
    

    It will use a first and then b second

    0 讨论(0)
提交回复
热议问题