用户名:ljj315686333
#include<st问答ring.h>
classstring
{
public:
string();
stri训ng(constchar*s);
string(conststring&s);
~s伯黄做河花旧决许tring();
//赋值运算符
conststring&operator=(constchar*s);
conststring&operator=(conststring&s);
//比较字符串是否相等
booloperator==(constchar*s)const;
booloperator==(conststring&s)const;
//比较字符倒串大小
booloper祖夫停才仍富胞ator<(constchar*s)const需素孔注至句口微注;
booloperato跑音将李科宽们与r<(conststring&s)const;
booloperat洋过免施关艺or>(constchar*s)const;
booloperator>(conststring&s)const;
//字符串长度
intlength()const胶围{returnm_length;}
//转化到字符指针
constchar*data()const{returnm_data;}
//清空字符串
voidclear();
private:
intm_length;
char*m_data;
};
string:间会科处运:string()
:m_length(0)
{
m_dat止师波语a=newchar[洋端显值写始1];
m_data[0]=市气先裂夫普企置探远'\0';
}
string::string(constchar*s)
{
m_length=strlen(s);
m_data=newchar[m_length+1];
memcpy(m_data,s,m_length+1);
}
string::string(conststring&s)
{
m_length=s.length();
m_data=newchar配诗击兵古积喜师张密改[m_length+1];
memcpy(m_data,s.data置演强听量山弱察(),m_length+1);
}
string::~string()
{
clear();
}
voidstring::clear()
{
m_length=0;
if(m_data)
delete[]m_含持新阳走轻车谁维灯data;
m_d我抗ata=**LL;
}
con判映育丰去阻味盐ststring&string::operator=(constchar*s)
{
clear();
m_length=strlen(s);
m_data=newchar[m_length+1];
memcpy(m_data,s,m_length+1);
return*this;
}
conststring&string::operator=(conststring&s)
{
clear();
m_length=s.lengt区h();
m_data=newchar[m_length+1];
memcpy(m_data,s.data(),m_length+1);
return*this;
}
boolstring::operator==(constchar*s)const
{
returnstrcmp(m_data,s)==0;
}
boolstring::operator==(conststring&s)const
{
returnm_length==s.length()&&strcmp(m_data,s.data())==0;
}
boolstring::operator<(constchar*s)const
{
returnstrcmp(m_data,s)<0;
}
boolstring::operator<(conststring&s)const
{
returnstrcmp(m_data,s.data())<0;
}
boolstring::operator>(constchar*s)const
{
returnstrcmp(m_data,s)<0;
}
boolstring::operator>(conststring&s)const
{
returnstrcmp(m_data,s.data())<0;
}