C++ string comparison
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
void main()
{
string a = "a";
string b(1, -70); /*constructor, create a string having 1 character that its value is equal to -70*/
cout<<((b>a)?b:a);
}
//output on screen: b was printed, not a (!)
Why b>a although the value of b is less than the value of a? How can I rectify this s开发者_如何转开发ituation?
On VS2010, I found that the char was signed - hence not the desired explanation. Stepping through the debugger on the comparison, I eventually hit the code:
template<> struct char_traits<char>
{ // properties of a string or stream char element
typedef char _Elem;
typedef _Elem char_type;
typedef int int_type;
typedef streampos pos_type;
typedef streamoff off_type;
typedef _Mbstatet state_type;
static int __CLRCALL_OR_CDECL compare(const _Elem *_First1, const _Elem *_First2,
size_t _Count)
{ // compare [_First1, _First1 + _Count) with [_First2, ...)
return (_CSTD memcmp(_First1, _First2, _Count));
}
// etc
};
So the real comparision boils down to a memcmp. Checking that out, we find "evaluated as unsigned char values", hence the problem.
Cf. Arytom's answer - Interesting, I did not know this. Looking it up:
The 1998 standard 21.1.3.1:6 on char_traits states 'lt' is defined identically to the built in operator <.
The N3126 draft, 21.2.3.1:5 states it should be as for unsigned char.
Strings are always compared as unsigned char
regardless of the signess of actual character.
std::string
took this behavior from C where strcmp uses characters in range 0-255 even if the char
is in range -128 -- 127.
So basically -70 is 186 and 186 > 'a'
Edit: Reference to standard. I don't have C++98/2003 near me just C++0x, but:
"Working Draft, Standard for Programming Language C++", N2915, 21.2.3.1, remark 5 says:
The two-argument members eq and lt shall be defined identically to the built-in operators == and < for type unsigned char.
i.e. characters compared as unsigned char. (it references to character traits specialization for char)
See the binary equivalent value of -70 as a 1 character.
精彩评论