This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
string::compare() problem?
- From: Andrew Walrond <andrew at walrond dot org>
- To: libstdc++ at gcc dot gnu dot org
- Date: Sun, 21 Mar 2004 16:17:26 +0000
- Subject: string::compare() problem?
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)