1、给昨天的My_string补充,字符串之间的比较和字符串的输入输出函数
#include <iostream>
#include <cstring>
using namespace std;
class My_string
{
private:
char* cstr;
int len;
public:
//12.6练习
My_string():cstr(NULL),len(0)//无参构造
{
}
My_string(const char* str)//有参构造
{
len = strlen(str);
cstr = new char[len+1];
strcpy(cstr,str);
len = strlen(cstr);
}
My_string(const My_string &other)//拷贝构造
{
this->len = other.len;
cstr = new char[other.len+1]();//深拷贝
strcpy(cstr,other.cstr);
}
My_string &operator=(const My_string &other)//拷贝赋值
{
if(this != &other)
{
if(this->cstr != NULL)
{
delete [] cstr;
}
this->cstr = new char[len+1];//深拷贝
strcpy(this->cstr,other.cstr);
this->len = other.len;
}
return *this;//返回自身引用
}
~My_string()//析构
{
if(cstr == NULL)
{
return;
}
delete cstr;//释放指针成员的空间
}
bool empty(){return (strlen(cstr) == 0 && len == 0);}//判空
int size(){return len;}//返回长度
char &at(int index){return cstr[index];}//定位
char* c_str(){return cstr;}//传化为C风格字符串
//12.7练习
// My_string
My_string operator+(const My_string &R)//二合一
{
My_string temp;
if(cstr != NULL)
{
temp.cstr = new char[this->len+R.len+1];
strcpy(temp.cstr,this->cstr);
strcat(temp.cstr,R.cstr);
}
else
{
temp.cstr = new char[R.len+1];
strcpy(temp.cstr,R.cstr);
}
temp.len = this->len+R.len;
return temp;
}
My_string &operator=(const char* R)//复制
{
if(cstr != NULL)
{
delete [] cstr;
}
cstr = new char[strlen(R)];
strcpy(cstr,R);
return *this;
}
//比较是否相等(3种)
bool operator==(const My_string &R)const
{
return strcmp(this->cstr,R.cstr);
}
bool operator==(const char* R)const
{
return strcmp(this->cstr,R);
}
bool operator==(const string &R)const
{
return strcmp(this->cstr,R.c_str());
}
//关系
bool operator>(const My_string &R)const
{
return strcmp(this->cstr,R.cstr)>0?1:0;
}
bool operator>(const char* R)const
{
return strcmp(this->cstr,R)>0?1:0;
}
bool operator>(const string &R)const
{
return strcmp(this->cstr,R.c_str())>0?1:0;
}
bool operator<(const My_string &R)const
{
return strcmp(this->cstr,R.cstr)<0?1:0;
}
bool operator<(const char* R)const
{
return strcmp(this->cstr,R);
}
bool operator<(const string &R)const
{
return strcmp(this->cstr,R.c_str())<0?1:0;
}
bool operator>=(const My_string &R)const
{
return strcmp(this->cstr,R.cstr)>=0?1:0;
}
bool operator>=(const char* R)const
{
return strcmp(this->cstr,R)>=0?1:0;
}
bool operator>=(const string &R)const
{
return strcmp(this->cstr,R.c_str())>=0?1:0;
}
bool operator<=(const My_string &R)const
{
return strcmp(this->cstr,R.cstr)<=0?1:0;
}
bool operator<=(const char* R)const
{
return strcmp(this->cstr,R)<=0?1:0;
}
bool operator<=(const string &R)const
{
return strcmp(this->cstr,R.c_str())<=0?1:0;
}
bool operator!=(const My_string &R)const
{
return !strcmp(this->cstr,R.cstr);
}
bool operator!=(const char* R)const
{
return !strcmp(this->cstr,R);
}
bool operator!=(const string &R)const
{
return !strcmp(this->cstr,R.c_str());
}
//插入、提取(双目)
friend ostream &operator<<(ostream &L,const My_string &R);
friend istream &operator>>(istream &L,My_string &R);
};
//插入、提取(双目)
ostream &operator<<(ostream &L,const My_string &R)
{
if(R.cstr == NULL)
{
L<<"空";
}
else
{
L<<R.cstr;
}
return L;
}
istream &operator>>(istream &L,My_string &R)
{
if(R.cstr != NULL)
{
delete []R.cstr;
}
R.cstr = new char[256];
L>>R.cstr;
R.len = strlen(R.cstr) ;
return L;
}
int main()
{
/* My_string s1;
cout<<s1.empty()<<endl;
My_string s2("my hello");
cout<<s2.empty()<<endl;
cout<<s2.size()<<endl;
cout<<s2.at(4)<<endl;
char* str = s2.c_str();
cout<<str<<endl;
*/
cout<<"*************************************************"<<endl;
My_string s10("this is my cat");
My_string s11("cat and dog");
My_string s12 = s10+s11;
cout<<s10<<" "<<s11<<" "<<s12<<endl;
return 0;
}