#include <iostream>
#include <cstring>
using namespace std;
class My_string
{
private:
char *cstr;
int len;
public:
My_string(const char *str=NULL)
{
if(str){ //有参构造
len=strlen(str);
cstr=new char[strlen(str)+1];
strcpy(cstr,str);
}else{ //无参构造
cstr=new char('\0');
len=0;
}
}
My_string(const My_string &other):len(other.len)//拷贝构造
{
cstr=new char[other.len+1];
strcpy(cstr,other.cstr);
}
My_string& operator=(const My_string &other)//拷贝赋值
{
if(this!=&other){
cstr=new char[other.len+1];
strcpy(cstr,other.cstr);
len=other.len;
}
return *this;
}
const My_string operator+(const My_string &other)const//实现+号运算符重载
{
My_string temp;
temp.len=this->len+other.len;
temp.cstr=new char[temp.len+1];
strcat(temp.cstr,this->cstr);
strcat(temp.cstr,other.cstr);
return temp;
}
My_string& operator+=(const My_string &other)//实现+=号运算符重载
{
this->len+=other.len;
strcat(this->cstr,other.cstr);
return *this;
}
bool operator==(const My_string &other)const//实现==号运算符重载
{
return strcmp(this->cstr,other.cstr)==0?1:0;
}
bool operator!=(const My_string &other)const//实现!=号运算符重载
{
return strcmp(this->cstr,other.cstr)!=0?1:0;
}
bool operator<(const My_string &other)const//实现<号运算符重载
{
return strcmp(this->cstr,other.cstr)<0?1:0;
}
bool operator>(const My_string &other)const//实现>号运算符重载
{
return strcmp(this->cstr,other.cstr)>0?1:0;
}
bool operator<=(const My_string &other)const//实现<=号运算符重载
{
return strcmp(this->cstr,other.cstr)<=0?1:0;
}
bool operator>=(const My_string &other)const//实现>=号运算符重载
{
return strcmp(this->cstr,other.cstr)>=0?1:0;
}
char &operator[](int i)//实现[]运算符重载
{
return this->cstr[i];
}
//将提取运算符重载函数设为友元
friend istream& operator>>(istream &L,My_string &R);
//将插入运算符重载函数设为友元
friend ostream& operator<<(ostream &L,const My_string &R);
//将getline重载函数设为友元
friend istream& getline(istream& is, My_string& s);
~My_string()
{
delete[] cstr;
}
bool empty()
{
return len==0?1:0;
}
int size()
{
return len;
}
char &at(int index)
{
if(index>=0)
return cstr[index];
else
return cstr[len];
}
char* c_str()
{
return cstr;
}
};
//实现提取运算符重载
istream& operator>>(istream &L,My_string &R){
L>>R.cstr;
R.len=strlen(R.cstr);
return L;
}
//实现插入运算符重载
ostream& operator<<(ostream &L,const My_string &R){
L<<R.cstr;
return L;
}
//实现getline重载
istream& getline(istream& is, My_string& s){
gets(s.cstr);
s.len=strlen(s.cstr);
return is;
}
void show(My_string s)
{
cout<<"**************************"<<endl;
cout<<"字符串长度为>>>"<<s.size()<<endl;
cout<<"字符串为>>>"<<s.c_str()<<endl;
}
int main()
{
My_string s("1234");
show(s);
My_string s1="hello";
show(s1);
My_string s2;
show(s2);
s2=s1;//测试=运算符
show(s2);
My_string s3=s+s1;
show(s3);
if(s1==s2)//测试==运算符
cout<<"s1和s2相等"<<endl;
else
cout<<"s1和s2不相等"<<endl;
if(s1>s)//测试>运算符
cout<<"s1大于s"<<endl;
else
cout<<"s1不大于s"<<endl;
My_string s4;
cout<<"(cin)请输入s4>>";
cin>>s4;//测试提取运算符
while(getchar()!=10);
show(s4);
cout<<s4<<endl;//测试插入运算符
for(int i=0;i<s4.size();i++)
printf("s4[%d]=%c ",i,s4[i]);//测试[]运算符
cout<<endl;
My_string s5="qwer";
s5+=s;//测试+=运算符
show(s5);
My_string s6;
cout<<"(getline)请输入s6>>";
getline(cin,s6);//测试getline
show(s6);
cout<<s6<<endl;
return 0;
}
仿照string类,实现自定义My_strng类,以及相关操作
于 2022-12-06 20:16:30 首次发布