解释标准库排序函数C++的比较谓词的工作原理?

Pra*_*rma -2 c++ sorting function c++11

在stackoverflow上阅读了一些答案后,我仍然无法理解何时比较函数必须返回false以及何时返回true.在这个答案中写道,它模拟小于运算符,但我现在认为,如果比较函数是这样的:

bool compare(const myClass& object1, const myClass& object2)
{
     if(object1.property < object2.property)
         return true;
     else
         return false;
}
Run Code Online (Sandbox Code Playgroud)

将按myclass升序对对象矢量进行排序.我是对的吗?...我觉得我还是很困惑.

hel*_*low 5

如有疑问,请阅读参考文献.

comp -

比较函数对象(即满足比较要求的对象),如果第一个参数小于(即在之前排序)第二个参数,则返回true.

比较函数的签名应等效于以下内容:

bool cmp(const Type1 &a, const Type2 &b);

签名不需要具有const&,但是函数对象不能修改传递给它的对象.Type1和Type2类型必须使得RandomIt类型的对象可以被解除引用,然后隐式转换为它们.