先看compare的源码
// 01. int compare(const string& obj) const; 调用了03int compare(const _Myt& _X) const{ return (compare(0, _Len, _X.c_str(), _X.size())); }// 02. int compare(unsigned int startO_id, unsigned int len, const string& obj) const; 调用了03int compare(size_type _P0, size_type _N0, const _Myt& _X) const{ return (compare(_P0, _N0, _X, 0, npos)); }// 03. int compare(unsigned int start_id, unsigned int len, const string& obj, unsigned int start_idobj, unsigned int len);int compare(size_type _P0, size_type _N0, const _Myt& _X,size_type _P, size_type _M) const{ if (_X.size() < _P) _Xran(); if (_X._Len - _P < _M) _M = _X._Len - _P; return (compare(_P0, _N0, _X.c_str() + _P, _M)); }// 04. int compare(const char * str); 调用了06int compare(const _E *_S) const{ return (compare(0, _Len, _S, _Tr::length(_S))); }// 05. int compare(unsigned int start_id, unsigned int len, const char* str); 调用了06int compare(size_type _P0, size_type _N0, const _E *_S) const{ return (compare(_P0, _N0, _S, _Tr::length(_S))); }// 06. int compare(unsigned int start_id, unsigned int len, const char* str, unsigned int len2);int compare(size_type _P0, size_type _N0, const _E *_S, size_type _M) const{ if (_Len < _P0) _Xran(); if (_Len - _P0 < _N0) _N0 = _Len - _P0; size_type _Ans = _Tr::compare(_Psum(_Ptr, _P0), _S, _N0 < _M ? _N0 : _M); return (_Ans != 0 ? _Ans : _N0 < _M ? -1 : _N0 == _M ? 0 : +1); }
测试例子源码
string str1 = "Hello";string str2 = "World";string str3 = "Hello";cout << "str1=" << str1 << "\t str2=" << str2 << "\t str3=" << str3 << endl;// 01. int compare(const string& obj) const; 调用了03cout << "int compare(const string& obj); " << endl;cout << "str1.compare(str3) -> " << str1.compare(str3) << endl;cout << "str1.compare(str2) -> " << str1.compare(str2) << endl;cout << "#####################################" << endl << endl;// 02. int compare(unsigned int start_id, unsigned int len, const string& obj) const; 调用了03cout << "int compare(unsigned int start_id, unsigned int len, " << "const string& obj) const;" << endl;cout << "str1.compare(0, 5, str3) -> " << str1.compare(0, 5, str3) << endl;cout << "str1.compare(1, 3, str3) -> " << str1.compare(1, 3, str3) << endl;cout << "str1.compare(1, 3, str2) -> " << str1.compare(1, 3, str2) << endl;cout << "#####################################" << endl << endl;// 03. int compare(unsigned int start_id, unsigned int len, const string& obj,\ unsigned int start_idobj, unsigned int len); cout << "int compare(unsigned int start_id, unsigned int len, const string& obj, " << "unsigned int start_idobj, unsigned int len); " << endl;cout << "str1.compare(0, 5, str3, 0, 5) -> " << str1.compare(0, 5, str3, 0, 5) << endl;cout << "str1.compare(0, 5, str2, 0, 5) -> " << str1.compare(0, 5, str2, 0, 5) << endl;cout << "#####################################" << endl << endl;// 04. int compare(const char * str); 调用了06 cout << "int compare(const char * str) const;" << endl;cout << "str1.compare(str3.c_str()) -> " << str1.compare(str3.c_str()) << endl;cout << "str1.compare(str2.c_str()) -> " << str1.compare(str2.c_str()) << endl;cout << "#####################################" << endl << endl;// 05. int compare(unsigned int start_id, unsigned int len, const char* str); 调用了06 cout << "int compare(unsigned int start_id, unsigned int len, const char* str) const;" << endl;cout << "str1.compare(0, 5, str3.c_str()) -> " << str1.compare(0, 5, str3.c_str()) << endl;cout << "str1.compare(0, 3, str2.c_str()) -> " << str1.compare(0, 3, str2.c_str()) << endl;cout << "#####################################" << endl << endl;// 06. int compare(unsigned int start_id, unsigned int len, const char* str, unsigned int len2);cout << "int compare(unsigned int start_id, unsigned int len, " << "const char* str, unsigned int len2) const;" << endl;cout << "str1.compare(0, 3, str3.c_str(), 3) -> " << str1.compare(0, 3, str3.c_str(), 3) << endl;cout << "str1.compare(0, 3, str2.c_str(), 3) -> " << str1.compare(0, 3, str2.c_str(), 3) << endl;cout << "#####################################" << endl << endl;
显示结果
可以看出,当Hello和World比的时候,返回的都是-1.
而当内容相同的时候,返回0.
可以得出结论,其实,compare是逐个元素,或者说是逐个字符比对的,当ch1>ch2, 返回1;当ch1