This is what I have right now (str is a dynamic, null-terminating char array):
bool String::operator<(const String& rhs) const {
if (str == rhs.str)
return false;
int i = 0;
while (str[i] != '\0' && rhs[i] != '\0') {
if (str[i] > rhs[i])
return false;
++i;
}
return true;
}
This passes most tests, but it fails on:
String s1("abc");
String s2("abcde");
assert(!(s2 < s1));
No matter how I alter the function it always seem to fail one test or another. How would YOU overload this operator? Basically I just need to compare two null-terminating char arrays and see which one is the lesser (without any libraries).