Re: debug assert error in a std::sort of vector<int>
lists@givemefish.com wrote:
std::sort(v1.begin(), v1.end(), not2(std::greater<int>()));
[I realize that I could use std::less<int> in this case, but I was
experimenting with not2.]
No, you couldn't use std::less! The point is that the opposite of "greater"
is "less or equal". In the sorting algorithm, two elements are considered
equal when (!comp(a,b) && !comp(b,a)), but for the less or equal
comparison, comp(a,a) returns true.
I get the assertion from within the sort function that says:
Expression: invalid operator <
I guess the check there is something like
assert(!comp(a,a));
i.e. comparing the same element to itself must return false.
The code appears to work fine if I ignore the assertion.
Try inserting several equal values into the vector and try again, I guess it
will end up in an endless loop.
Also, the code compiles and runs under gcc.
Other than under VC8, you need to explicitly activate the debug mode of
recent GNU libstdc++s, see the documentation for info how to do that. The
same btw applies to STLport's debug mode.
Uli
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]