不强调效率,以时间换空间,可读性差
class String
{
public:
String():_str(new char[1]){*_str = '\0';}
String(const char* s):_str(new char[strlen(s)+1]){strcpy(_str, s);}
String(const String& s):_str(new char[strlen(s._str)+1]){strcpy(_str, s._str);}
~String(){delete[] _str;}
String& operator=(String s){std::swap(_str,s._str);return *this;}
size_t Size()const{return strlen(_str);}
size_t Lengh()const{return strlen(_str);}
bool operator>(const String& s){return((strcmp(_str,s._str)==1)?1:0);}
bool operator<(const String& s){return((strcmp(_str,s._str)==-1)?1:0);}
bool operator>=(const String& s){return((strcmp(_str,s._str)!=(-1))?1:0);}
bool operator<=(const String& s){return((strcmp(_str,s._str)!=(1))?1:0);}
bool operator==(const String& s){return((strcmp(_str,s._str)==0)?1:0);}
bool operator!=(const String& s){return!(*this==s);}
char operator[](size_t i){return *((*this)._str+i);}
private:
char* _str;
};
未调用系统函数版,可读性差
bool operator>(const String& s){if((_str!=NULL)||(s._str!=NULL)){char* s1=_str,*s2=s._str;while((*s1!='\0')&&(*s2!='\0')){if(*s1<*s2||*s1==*s2)return 0;s1++;s2++;}return (*s2=='\0');}return 0;}
bool operator<(const String& s){return !((*this>s)||(*this==s));}
bool operator==(const String& s){if((_str!=NULL)&&(s._str!=NULL)){char*s1=_str,*s2=s._str;while((*s1!='\0')&&(*s2!='\0')){if(*s1!=*s2)return 0;s1++;s2++;}return 1;}return 0;}
bool operator!=(const String& s){return (!(*this==s));}
【作者:果冻 http://blog.youkuaiyun.com/jelly_9】