函数
#include <iostream>
#include <cstring>
using namespace std;
class My_string
{
private:
char *cstr;
int len;
public:
My_string():cstr(NULL),len(0)
{
}
My_string(const char* str)
{
len = strlen(str);
cstr = new char[len+1] ();
strcpy(cstr, str);
}
My_string(const My_string &other)
{
len = other.len;
cstr = new char[len+1]();
strcpy(cstr, other.cstr);
}
~My_string()
{
if(len == 0)
{
delete []cstr;
cstr = NULL;
}
}
bool empty()
{
return len==0?1:0;
}
int size()
{
return len;
}
char &at(int index)
{
return cstr[index];
}
char* c_str()
{
return cstr;
}
My_string &operator+(const My_string &R)
{
if(cstr != NULL)
{
My_string temp = *this;
delete [] cstr;
cstr = new char[len+R.len+1];
strcpy(cstr, temp.cstr);
strcat(cstr, R.cstr);
}
else
{
cstr = new char[R.len+1];
strcpy(cstr, R.cstr);
}
len +=R.len;
return *this;
}
My_string &operator=(const My_string &R)
{
if(cstr != NULL)
delete [] cstr;
cstr = new char[R.len+1];
strcpy(cstr, R.cstr);
return *this;
}
My_string &operator=(const char* s)
{
if(cstr != NULL)
delete [] cstr;
cstr = new char[strlen(s)];
strcpy(cstr, s);
return *this;
}
bool operator==(const My_string &R)const
{
return !strcmp(cstr, R.cstr);
}
bool operator==(const char* s)const
{
return !strcmp(cstr, s);
}
bool operator ==(const string &s)const
{
return !strcmp(cstr, s.c_str());
}
bool operator!=(const My_string &R)const
{
return strcmp(cstr, R.cstr);
}
bool operator>(const My_string &R)const
{
return strcmp(cstr, R.cstr) >0 ? 1:0;
}
bool operator<(const My_string &R)const
{
return strcmp(cstr, R.cstr) <0 ? 1:0;
}
bool operator>=(const My_string &R)const
{
return strcmp(cstr, R.cstr) >=0 ? 1:0;
}
bool operator<=(const My_string &R)const
{
return strcmp(cstr, R.cstr) <=0 ? 1:0;
}
char &operator[](int i)
{
if(i<0 || i>=len)
{
static char c = ' ';
cout<<"数组下标越界"<<endl;
return c;
}
return cstr[i];
}
friend ostream &operator<<(ostream &o,const My_string &s);
friend istream &operator>>(istream &i,My_string &s);
};
ostream &operator<<(ostream &o,const My_string &s)
{
if(s.cstr == NULL)
o<<"字符串为空";
else
o<<s.cstr;
return o;
}
istream &operator>>(istream &i,My_string &s)
{
if(s.cstr != NULL)
delete [] s.cstr;
s.cstr = new char[280];
i>>s.cstr;
s.len = strlen(s.cstr);
return i;
}
int main()
{
My_string s1("hello");
My_string s2 = s1;
My_string s3(s1);
My_string s4;
cout<<"&s1 = "<<&s1<<" &s2 = "<<&s2<<" &s3s = "<<&s3<<" &s4 = "<<&s4<<endl;
cout<<"------------------------------------------"<<endl;
cout<<"s1 = "<<s1.c_str()<<endl;
cout<<"s2 = "<<s2.c_str()<<endl;
cout<<"s3 = "<<s3.c_str()<<endl;
cout<<"------------------------------------------"<<endl;
cout<<"s3[4] = "<<s3.at(4)<<" s3.size = "<<s3.size()<<" s3.empty = "<<s3.empty()<<endl;
cout<<"s4.empty = "<<s4.empty()<<endl;
char* str = s3.c_str();
cout<<"char* str = "<<str<<endl;
cout<<"------------------------------------------"<<endl;
My_string r1("world");
cout<<"s1 + r1 = "<<(s1+r1).c_str()<<endl;
s2 = s1;
cout<<"s2 = "<<s2.c_str()<<endl;
cout<<"------------------------------------------"<<endl;
cout<<"s2 == s1 ? "<<boolalpha<<(s2==s1)<<endl;
cout<<"s2 == s3 ? "<<boolalpha<<(s2==s3)<<endl;
cout<<"s2 == helloworld ? "<<boolalpha<<(s2=="helloworld")<<endl;
cout<<"------------------------------------------"<<endl;
cout<<"s2 != s1 ? "<<boolalpha<<(s2!=s1)<<endl;
cout<<"s2 != s3 ? "<<boolalpha<<(s2!=s3)<<endl;
cout<<"------------------------------------------"<<endl;
cout<<"s3 > s1 ? "<<boolalpha<<(s3>s1)<<endl;
cout<<"s3 < s1 ? "<<boolalpha<<(s3<s1)<<endl;
cout<<"s3 >= s1 ? "<<boolalpha<<(s3>=s1)<<endl;
cout<<"s3 <= s1 ? "<<boolalpha<<(s3<=s1)<<endl;
cout<<"------------------------------------------"<<endl;
cout<<"s3[4] = "<<s3[4]<<endl;
cout<<"s3[4]+1 = "<<s3[4]+1<<endl;
cout<<"s3[5] = "<<s3[5]<<endl;
My_string o;
cout<<"s1= "<<o<<endl;
cout<<"s1= "<<s1<<endl;
My_string in;
cout<<"请输入字符串"<<endl;
cin>>in;
cout<<"in = "<<in<<endl;
return 0;
}
测试结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/92f6284d75bdce339404b1b50d350c4e.png)