Custom Sorting a vector of tuples

前端 未结 4 416
自闭症患者
自闭症患者 2021-02-04 04:22

I have a vector of tuples like

vector> v;

I believe that when the default comparison kicks in for tuple types, i

4条回答
  •  深忆病人
    2021-02-04 04:56

    You can do it like this

    #include 
    #include 
    #include 
    #include 
    using namespace std;
    
    vector> v;
    
    int main() {
      v.push_back(std::make_tuple(1,1.2,'c'));
      v.push_back(std::make_tuple(1,1.9,'e'));
      v.push_back(std::make_tuple(1,1.7,'d'));
    
      sort(v.begin(),v.end(),
           [](const tuple& a,
           const tuple& b) -> bool
           {
             return std::get<1>(a) > std::get<1>(b);
           });
    
      cout << std::get<2>(v[0]) << endl;
      return 0;
    }
    

提交回复
热议问题