string::compare() problem?
Andrew Walrond
andrew@walrond.org
Sun Mar 21 16:26:00 GMT 2004
Test program (Using gcc 3.3.3)
#include <string>
#include <iostream>
using namespace std;
int main()
{
string a("--level");
string b("--");
cout << a.compare(b) << endl;
return 0;
}
Output:
5
Why doesn't this give 0? According to my reading of the function, it should
only compare the first 2 chars of each string.
From basic_string.h:
int
compare(const basic_string& __str) const
{
size_type __size = this->size();
size_type __osize = __str.size();
size_type __len = std::min(__size, __osize);
int __r = traits_type::compare(_M_data(), __str.data(), __len);
if (!__r)
__r = __size - __osize;
return __r;
}
(I'm not subscribed, so please CC me on replies)
More information about the Libstdc++
mailing list