个人主页:Jason_from_China-CSDN博客
所属栏目:C++系统性学习_Jason_from_China的博客-CSDN博客
所属栏目:C++知识点的补充_Jason_from_China的博客-CSDN博客
string模拟实现比较大小
模拟实现比较我们主要依赖的是strcmp函数
//比较大小//比较大小不需要重载为成员函数bool operator==(const string& s1, const string& s2){return strcmp(s1.c_str(), s2.c_str()) == 0;}bool operator!=(const string& s1, const string& s2){return !(s1 == s2);}bool operator>(const string& s1, const string& s2){return strcmp(s1.c_str(), s2.c_str()) > 0;}bool operator<(const string& s1, const string& s2){return !(s1 > s2 && s1 == s2);}bool operator>=(const string& s1, const string& s2){return s1 == s2 || s1 > s2;}bool operator<=(const string& s1, const string& s2){return s1 == s2 || s1 < s2;}
注意事项:
- 这里的代码没有什么好解释的,主要就是实现大于和等于,其他的都是复用
- 复用的好处就在于如果我们要改变所有的代码,我们只需要改变核心的大于和等于就可以
- 这里返回的是bool值