设计一个string类

//实现字符串的基本操作(构造函数,复制构造函数,求子串,=重载,+=重载,匹配)。
#include
#include
#include
using namespace std;
const int maxlen=128;
class string
{
public:
string(const string& ob);
string(const char*init);
string();
~string (){delete[]ch;}
int length()const {return curlen;}//常量函数,不能修改成员变量
string &operator()(int pos,int len);
int operator ==(const string&ob)const{return strcmp(ch,ob.ch )==0;}
int operator!=(const string &ob)const{return strcmp(ch,ob.ch )!=0;}
int operator!()const{return curlen==0;}
string & operator=(const string &ob);//返回引用不会新建对象,但返回对象时要创建新的对象
string &operator+=(const string &ob);
char &operator[](int i);
int find(string &pat)const;
private:
int curlen;
char *ch;
};
int main()
{
string e;                                          //无参构造函数调用
string a="abcdef";                                 //=操作符重载调用
cout<<"string a:"<
for (int i=0;i
{
cout<
}
cout<
string d("as");
cout<<"string d:"<
for (i=0;i
{
cout<
}
cout<
a+=d;                                               //+=操作符重载调用
cout<<"string a+=d:"<
for (i=0;i
{
cout<
}
string b(a);
cout<<<"string b(a) "<<"string b:"<
for (i=0;i
{
cout<
}
cout<
cout<<"compare a and b:"<
if (a==b) {cout<<"a equals b!"<
}
else
cout<<"not equal!"<
string c("def");
cout<<"c:"<
for (i=0;i
{
cout<
}
cout<
if (a.find(c)==-1) {cout<<"匹配不成功!"<
}
else
cout<<"匹配成功!"<
if (c!=b) {cout<<"c!=b  "<
return 0;
}

 


string::string(const string &ob)
{
ch=new char [maxlen+1 ];
if(!ch)
{
cerr<<"allocation error/n";
exit(1);
}
curlen=ob.curlen;
strcpy(ch,ob.ch );
}


string::string(const char*init)
{                                                        
ch=new char [maxlen+1];
if(!ch)
{
cerr<<"allocation error/n";
exit(1);
}
curlen=strlen(init);
strcpy(ch,init);
}

//写个新的版本

string::string( const char *str){

if(str==null){

ch=new char[1];

*ch=‘/0‘;

}

else{

int length=strlen(str);

ch=new char[length+1];// 这里最好加一个null判断,也就是判断ch==null,从而看内存分配有没有失败

strcpy(ch,str);

}

}


string::string()
{
ch=new char[maxlen+1];
if(!ch)
{cerr<<"allocation error/n";exit(1);}
curlen=0;
ch[0]='/0';
}


string&string::operator ()(int pos,int len)
{
string *temp=new string;
if(pos<0||pos+len-1>=maxlen||len<0)
{
temp->curlen=0;
temp->ch[0]='/0';
}
else
{
if(pos+len-1>=curlen)
  len=curlen-pos;
temp->curlen=len;
for(int i=0,j=pos;i
  temp->ch[i]=ch[j];
temp->ch[len]='/0';
}
return *temp;
}


string&string::operator =(const string &ob)
{
if(&ob!=this)
{
delete[]ch;
ch=new char [maxlen+1];
if(!ch)
{
  cerr<<"out of memory!/n";
  exit(1);
}
curlen=ob.curlen;
strcpy(ch,ob.ch );
}
else
cout<<"attempted assignment of a string to itself!/n";
return *this;
}

//这里也自己重写一个版本

string&string::operator =(const string &ob)
{

if( this==&ob) return *this;

delete [] ch;

int length=strlen(ob.ch);//这里是取不出来的.要写一个函数,这里只是用来简单表示一下

ch=new char[length+1];

strcpy(ch,os.ch);

return *this;

}

上面这个版本如果在new时出现异常,则this->ch已经被删掉了…是有问题的

//再写一个版本,这里主要 先是对输入对象做一个复件,然后再删除原先的ch,指向新构造的复件

string&string::operator =(const string &ob)
{

char * temp = ch;//先将原先ch的地址保存下来

//然后再新建一个对象string,以ob为参数。

//接着删掉temp

return *this;

}

//还可以用copy and swap 技术,就是先以输入对象为参数新建一个局部对象,然后再将新对象与原对象交换,接着返回*this,那个局部对象则自动析构。


string &string::operator +=(const string&ob)
{
char*temp=ch;
curlen+=ob.curlen;
ch=new char [maxlen+1];
if(!ch)
{
cerr<<"out of memory/n";
exit(1);
}
strcpy(ch,temp);
strcat(ch,ob.ch );
delete[]temp;
return *this;
}
char&string::operator [](int i)
{
if(i<0&&i>=curlen)
{
cout<<"out of memory/n";
exit(1);
}
return ch[i];
}
int string::find(string&pat)const
{
char *p=pat.ch ,*s=ch;int i=0;
if (*p&&*s) {while (i<=curlen-pat.curlen) {if (*p++==*s++) {if (!*p) {return i;
}
} else {
i++;
s=ch+i;
p=pat.ch ;
}
}
}
return -1;
}

你可能感兴趣的:(c/c++)