How to sort with a lambda?

后端 未结 3 2020
旧巷少年郎
旧巷少年郎 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:19

    Got it.

    sort(mMyClassVector.begin(), mMyClassVector.end(), 
        [](const MyClass & a, const MyClass & b) -> bool
    { 
        return a.mProperty > b.mProperty; 
    });
    

    I assumed it'd figure out that the > operator returned a bool (per documentation). But apparently it is not so.

提交回复
热议问题