std::sort behavior with ints that are equal
std::sort
doesn't preserve the order of the equivalent elements, std::stable_sort
does. However, in case of int
's you will not notice the difference unless you use some non-trivial ordering as in the following example:
struct half_less
{
bool operator()(int a, int b) const { return (a / 2) < (b / 2); }
};
std::sort(begin, end, half_less());
Here is another example when std::stable_sort
is a more suitable candidate than std::sort
@vitaut is right. I just want to add that you would not notice if the order of equal integers is changed. This only matters if you sort values which happen to have an indentifying property. For example if you store pointers to integers and sort by the integer value.