Re: problem with C++ sort algorithm
Folks,
This strange problem is now solved, thanks to a programming illiterate
(but very sharp OR guy) at work. I guess showing you guys my compare
method would have helped you point it out too:
bool compareByCloseAndAlloc( State* s1, State* s2 )
{
int retVal = s2->getPDClose() - s1->getPDClose();
if ( retVal != 0 ) return retVal < 0;
return s1->getAllocation() - s2->getAllocation();
}
Well, this guy asked me why i'm returning a boolean (retVal < 0) on
line 2, but an integer on line 3. I said, well, C++ really uses ints as
bool, and that i could change the code to
bool compareByCloseAndAlloc( State* s1, State* s2 )
{
int retVal = s2->getPDClose() - s1->getPDClose();
if ( retVal != 0 ) return retVal < 0;
retVal = s1->getAllocation() - s2->getAllocation();
return retVal < 0;
}
but it wouldn't make a shred of a difference. And it did. It fixed the
problem.
I still honestly believe i did no wrong (i thought in C++, 0 was false,
every other number in the universe means true). before we close this
inane topic, maybe someone could clarify?
Thanks again for all your help.
Ron Natalie wrote:
nthali wrote:
Now none of the objects in my vector are NULL, so i don't know how it
could possibly get a NULL obj2 in the compare method?
Are you sure? Are you sure you don't "resize" or otherwise
introduce default contstucted elements into the vector?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]