I am trying to rewrite the string class and i am having a little trouble overloading all the rational operators. Here is the code i have for the less than operator.
length is an integer that represents the length of the string object.
first is a pointer to the first character of the string.
Code:
bool String::operator< (const String& a) const
{
int temp = difference(a);
if(temp == length && length < a.length) //Testing to see if the leftString is a prefix of rightString
return true;
if(temp == a.length && a.length < length)
return false;
if((int)*(first+temp) < (int)*(a.first+temp))
return true;
else
return false;
}
Can anyone explain to me why this function is returning:
Comparing "part" and "particular"
< False
<= False
> False
>= False
== False
!= True
When clearly part is a prefix of particular
difference() returns the index of where the two strings differ.
Code:
int String::difference(String a) const
{
int placeholder = a.length+1;
for(int x = 0; x < a.length; x++)
{
if(first+placeholder != a.first+placeholder)
{ if(placeholder > x)
placeholder = x;
}
}
return placeholder;
}
Thank you all who ltry to help me with this problem.