Compare

Hi,

I've run this code, with the answers in the comments on the right. Is anyone able to explain how these numbers are calculated. I understand I'm comparing string S to the string "BCDEF", and understand the result will be 0 if they're equal, but I'm stumped on the below!

string S = "ABCDEF";

cout << S.compare(1,1,"BCDEF") << endl; //-4
cout << S.compare(2,1,"BCDEF") << endl; // 1
cout << S.compare(3,1,"BCDEF") << endl; // 2
cout << S.compare(4,1,"BCDEF") << endl; // 3
cout << S.compare(5,1,"BCDEF") << endl; // 4
cout << S.compare(6,1,"BCDEF") << endl; // -5

Thanks
The value is not important. It may not be the same value on all compilers and plaforms.

google the compare function. All you are assured is:

1
2
3
4
value	relation between compared string and comparing string
0	They compare equal
<0	Either the value of the first character that does not match is lower in the compared string, or all compared characters match but the compared string is shorter.
>0	Either the value of the first character that does not match is greater in the compared string, or all compared characters match but the compared string is longer.
Topic archived. No new replies allowed.