string函数_C++[09] string成员函数之比较函数compare以及其他未重载的函数

先看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;

显示结果

4381f5416e281c9ec712d7519ed296d1.png

可以看出,当Hello和World比的时候,返回的都是-1. 

而当内容相同的时候,返回0.

可以得出结论,其实,compare是逐个元素,或者说是逐个字符比对的,当ch1>ch2, 返回1;当ch1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值