std :: sort with comparison function

3

Studying a little about STL I came across the std::sort function that can receive a comparison function.

template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );

How does this comparison function work? And how is it used?

    
asked by anonymous 21.04.2017 / 00:15

1 answer

3

In C ++ std :: sort is an "algorithm", not a function. That is, std :: sort is a generic function declared as a template.

The use of the comparison function is necessary when there is no intrinsic order to be used when comparing the elements. This intrinsic order would be defined by the operator "<".

In the example below, it is not possible to compare two instances of the Person class: it is not possible to do "if (p1

21.04.2017 / 01:00