C++ STL - How does the third argument of the STL sort() work?

后端 未结 4 411
慢半拍i
慢半拍i 2020-12-18 01:14

I wish to sort an array of objects of class Person on the basis of its data member \'age\'. I store the objects in a vector v

4条回答
  •  生来不讨喜
    2020-12-18 01:40

    1. You simply provide functor, object that has operator() and is used while comparing objects.
    2. As far as I know, sort accepts functors, functions and lambda expressions to use them to compare objects.
    3. In method 3 you doesn't provide functor, function or lambda expression to compare so it's by default sorted using standard operator<. Obviously it doesn't sort your Person objects by age, as you would like to. In my opinion the cleanest form is method 4, using lambda expression.

提交回复
热议问题