A few days ago, I received a very serious bug in the game. When I executed an operation, it would become stuck. At the end of the debugging, locate the STD sort function and find that the function crashes as soon as it enters the debugging process. It is very likely that an endless loop occurs in it. At the beginning, I was surprised. Well-known, well-tested STLAlgorithmWill there be an endless loop? As a result, Baidu found many people on the Internet have encountered similar situations. Most of the endless loops occur because the comparison function in the sort function returns true for equal elements.
The sort function of STD uses the quicksort algorithm and uses piecewise recursive sorting. It is likely that a problem occurs during recursion, resulting in an infinite recursive loop. Later I sawThere are 21st items in keep Itve STL:Always let the comparison function return false for the same element.However, there is also a more safe way to use the stable_sort function, which is a stable sorting algorithm that maintains the order when two elements are equal. It is verified that this function will not cause an endless loop in any case. However, this function has a higher overhead than the sort function.