Re: std::sort
Jeff Schwab wrote:
On Nov 26, 2:38 pm, Mark P <use...@fall2005REMOVE.fastmailCAPS.fm>
wrote:
Jeff Schwab wrote:
Would std::sort ever compare an object with itself? I'm not talking
about two distinct, equal-valued objects, but rather this == &that.
The container being sorted is a std::vector.
I can't think of any reason why it *would* compare an object with
itself, but I don't know any reason why it *can't*.
That's what I told the coworker. Not very satisfying, is it? :)
May I ask why this matters? Is the code unstable against these sorts of
comparisons, is it an efficiency concern, or something else?
This ought to be
easy to check for your specific implementation by either looking at the
header file or by writing a comparison functor that will report if this
happens.
We know it's happening, because our comparison function is telling us
so. Just "looking" at the header file -- I assume you mean the
template implementation file #included by <algorithm> -- is one thing,
but understanding it is quite another. My (mis?)understanding of
std::sort is that it's a modified form of qsort that switches to
something like shellsort for particular sorts (no pun) of input.
Agreed that this is probably not a trivial task. The details will vary
by implementation but I took a quick glance at mine. It appears to be
only a couple hundred lines of code to digest so, if it's really
important to know, I think it's doable in fairly short order. If you
have a good debugger that will let you step through the header file
implementation then you may find it even easier.