Is it possible for std::sort to lead to a bug? [closed]
My Question is related to this as I solved the problem, I wrote my own sorting algorithm (a simple insertion sort), 开发者_开发知识库and it works. I am quite surprised by this as I thought that the standard library is well-tested. Are there any known special cases in which std::sort
might mess up?
No, it's highly unlikely that there are any known bugs in any of the common C++ standard library implementations of std::sort
. It's rigorously tested.
If you see a crash or incorrect results, it's almost certainly because you didn't adhere to the contract: either you passed in invalid arguments, or your comparator does not obey the requirements for a strict weak ordering (irreflexivity, asymmetry, transitivity, and transitivity of equivalence).
If your comparison function/object doesn't follow a strict weak ordering, or the thing you're sorting contains pointers that are no longer valid, either of those could cause it to break.
精彩评论