How to sort with a lambda?

后端 未结 3 2051
旧巷少年郎
旧巷少年郎 2020-12-12 12:51
sort(mMyClassVector.begin(), mMyClassVector.end(), 
    [](const MyClass & a, const MyClass & b)
{ 
    return a.mProperty > b.mProperty; 
});
         


        
3条回答
  •  孤街浪徒
    2020-12-12 13:14

    To much code, you can use it like this:

    #include
    #include
    
    int main()
    {
        std::array vec = { 1,2,3,4,5,6,7,8,9 };
    
        std::sort(std::begin(vec), 
                  std::end(vec), 
                  [](int a, int b) {return a > b; });
    
        for (auto item : vec)
          std::cout << item << " ";
    
        return 0;
    }
    

    Replace "vec" with your class and that's it.

提交回复
热议问题