Reputation: 136
I'm trying to use the following comparison function for sorting a vector<string>
, in which each string is a number (possibly large number upto 10^100). But I'm getting a debug assertion failure error in VS2010.
bool compareNumberStrings (const string &a, const string &b)
{
if (a.length () < b.length ())
return true;
if (a.length () == b.length ())
{
int i;
for (i=0; i<a.length() && a[i] == b[i]; i++);
if (i != a.length ())
return a[i] < b[i];
else
return true;
}
return false;
}
Upvotes: 0
Views: 95
Reputation: 15872
You could stop storing numbers in strings and use a big number library (e.g. GMP MP)
Upvotes: 0
Reputation: 71989
You're returning true
when the strings are completely equal. This is a violation of the requirements: the function must define a strict weak ordering, i.e. f(x, x)
must return false
. Your function returns true
, and the library might well contain a check that this doesn't happen.
Upvotes: 6