This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]