string类
#include <iostream>
#include <cstring>
using namespace std;
class mystring{
private:
char *str;//记录c风格的字符串
int size;//记录字符串的实际长度
public:
//无参构造
mystring():size(10)
{
str=new char[size];
strcpy(str,"");
}
//有参构造
mystring(const char*s)
{
size=strlen(s);
str=new char[size+1];
strcpy(str,s);
}
//拷贝构造
mystring(mystring & other):str(new char(*(other.str))),size(other.size)
{
strcpy(str,other.str);
}
//拷贝赋值
mystring &operator =(const mystring & other)
{
//排除给自己赋值
if(this!=&other)
{
delete str;
this->size=other.size;
this->str=new char[size+1];
strcpy(this->str,other.str);
}
return *this;
}
//析构函数
~mystring()
{
delete str;
}
//判空函数
bool empty()
{
if(strlen(str)==0)
{
return true;
}
else
{
return false;
}
}
//size函数
int length()
{
return size;
}
//c_str函数
char* c_str()
{
return str;
}
//at函数
char &at(int pos)
{
if(pos>=size||pos<0)
{
cout<<"error pos"<<endl;
}
return str[pos];
}
//成员函数实现[]重载
char &operator [](const int &pos)const
{
if(pos<=size && pos>=0)
{
return str[pos];
}
else
{
cout<<"error pos"<<endl;
return str[pos];
}
}
//成员函数实现+重载
const mystring operator +(const mystring &other)const
{
mystring t;
t.size=this->size+other.size;
t.str=new char[t.size+1];
strcpy(t.str,this->str);
strcat(t.str,other.str);
return t;
}
//成员函数实现+=重载
mystring &operator +=(const mystring & other)
{
//判断s1是否等于他本身
if(this == &other)
{
char* t=new char[this->size]; //申请空间存储s1
strcpy(t,this->str); //拷贝s1
delete []this->str; //释放原来存储s1的空间
this->size=other.size+this->size; //size=s1的size+s2的size
this->str=new char[this->size+1]; //申请空间存储s1+s2
memset(str,0,this->size);
strcpy(this->str,t); //拷贝s1
strcat(this->str,t); //链接s2
delete []t; //释放用于存储s1的空间
}
else
{
char* t=new char[this->size]; //申请空间存储s1
strcpy(t,this->str); //拷贝s1
delete []this->str; //释放原来存储s1的空间
this->size=other.size+this->size; //size=s1的size+s2的size
this->str=new char[this->size+1]; //申请空间存储s1+s2
memset(str,0,this->size);
strcpy(this->str,t); //拷贝s1
strcat(this->str,other.str); //链接s2
delete []t; //释放用于存储s1的空间
}
return *this;
}
//成员函数实现==重载
bool operator ==(const mystring &other)const
{
if(0==strcmp(this->str,other.str))
{
return true;
}
else
{
return false;
}
}
//成员函数实现!=重载
bool operator !=(const mystring &other)const
{
if(0!=strcmp(this->str,other.str))
{
return true;
}
else
{
return false;
}
}
//成员函数实现<重载
bool operator <(const mystring &other)const
{
if(strcmp(this->str,other.str)<0)
{
return true;
}
else
{
return false;
}
}
//成员函数实现>重载
bool operator >(const mystring &other)const
{
if(strcmp(this->str,other.str)>0)
{
return true;
}
else
{
return false;
}
}
friend istream &operator>>(istream &L,mystring &other);
friend ostream &operator<<(ostream &cout,mystring &other);
};
//全局函数实现>>重载
istream & operator>>(istream &L,mystring &other)
{
char s[other.length()];
cin>>s;
strcpy(other.c_str(),s);
return L;
}
//全局函数实现<<重载
ostream & operator<<(ostream &cout,mystring &other)
{
cout<<other.str<<endl;
return cout;
}
int main()
{
//有参构造
mystring str1("Hello World!");
cout<<"str1:"<<str1.c_str()<<endl;
cout<<endl;
//拷贝构造
mystring str2(str1);
cout<<"str2:"<<str2<<endl;
cout<<endl;
// //判空
// cout<<"empty:"<<boolalpha<<str2.empty()<<endl;
// cout<<endl;
// //size函数
// cout<<"size:"<<str2.length()<<endl;
// cout<<endl;
// //c_str函数
// cout<<"c_str:"<<str2.c_str()<<endl;
// cout<<endl;
// //at函数
// str2.at(4)='O';
// cout<<"at(4):"<<str2.at(4)<<endl;
// cout<<"str2:"<<str2.c_str()<<endl;
// cout<<endl;
// +
mystring str3;
str3=str1+str2;
cout<<"str1+str2:"<<str3<<endl;
cout<<endl;
//+=
cout<<"str1+=str1:"<<(str1+=str1)<<endl;
cout<<endl;
cout<<"str2+=str1:"<<(str2+=str1)<<endl;
cout<<endl;
// <
cout<<"str1<str2:"<<boolalpha<<(str1<str2)<<endl;
cout<<endl;
// >
cout<<"str1>str2:"<<boolalpha<<(str1>str2)<<endl;
cout<<endl;
// =
cout<<"str1=str2:"<<boolalpha<<(str1==str2)<<endl;
cout<<endl;
// !=
cout<<"str1!=str2:"<<boolalpha<<(str1!=str2)<<endl;
cout<<endl;
return 0;
}