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

后端 未结 3 1813
陌清茗
陌清茗 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:14

    The comparison predicate should return a bool: true if a < b and false otherwise. Change the return statement to:

      return a < b;
    

    Not to be confused with C-style 3-way comparison functions.

    NOTE: Though C++20 does introduce a 3-way comparison operator <=>, sort would still expect a 2-way compare predicate.

提交回复
热议问题