Should std::sort work with lambda function in c++0x/c++11?

后端 未结 3 1822
陌清茗
陌清茗 2020-12-17 08:43

I tried to use lambda function with sort, but was getting \"Segmentation fault\" errors. I managed to simplify the code to the following:

#inclu         


        
3条回答
  •  孤街浪徒
    2020-12-17 09:01

    The predicate is supposed to implement a simple, weak ordering. Also your range is off if you want to sort the entire thing. (I missed that that was intentional.) So all in all we're looking for something like this:

    std::sort(intArr, intArr + nelems, [](int a, int b){ return a < b; });
    

    Or even:

    std::sort(intArr, intArr + nelems);
    

    The default predicate for sorting is std::less, which does exactly what the lambda does.

提交回复
热议问题