模拟实现C++中的string类

来源:互联网 发布:腾讯视频播放器mac 编辑:程序博客网 时间:2024/06/15 17:30

一、用一般方法实现string类

c<span style="font-size:24px;">lass String{friend ostream& operator<<(ostream& os,String& str);public:    String (const char* str="")                      //构造函数:_capacity(size(str)+1),_sz(size(str)),_str(new char[_capacity]){strcpy(_str,str);}String(const String& s)                     //拷贝函数(深拷贝):_sz(s._sz ),_capacity(s._capacity ),_str(new char[s._capacity]){strcpy(_str,s._str);}String& operator= (String& s)              //把字符串s赋给当前字符串{    std::swap (_sz,s._sz );std::swap(_capacity,s._capacity );std::swap(_str,s._str );return *this;}char& operator[](int index)               {return _str[index];                //_str[3]='w'}char *c_str(){return _str;                       //返回字符串首地址}void PushBack(const char c)                //尾插{CheckCapacity(1);                  //检测容量_str[_sz++]=c;_str[_sz]='\0';}~String ()                                 //析构函数{if(_str!=NULL){_sz=0;_capacity=0;delete[] _str;_str=NULL;}}private:void CheckCapacity(int count){if(_sz+count >=_capacity){int Newcapacity=(2*_capacity>_sz+count)?2*_capacity:_sz+count;char *tmp=new char[Newcapacity];strcpy(tmp,_str);delete[] _str;_str=tmp;_capacity=Newcapacity;}   }int size(const char *str)          {return strlen(str);}private:char *_str;int _sz;int _capacity;}; ostream& operator<<(ostream& os,String& str)     //输出流{os<<str._str  ;        return os;}void test(){String str1("abcdef");String str2;str2=str1;str2[3]='w';str2.PushBack('g');cout<<str2<<endl;}</span>
二、用写时拷贝的方法实现string类

开辟了两块地址,一个用来存字符串,另一个用来计数。

class String{friend ostream& operator<<(ostream& os,String& str);public:String(const char* str=""):_str(new char[strlen(str)+1]),_pcount(new int[1]){*_pcount=1;strcpy(_str,str);}String(const String& s){_pcount=s._pcount ;++(*_pcount);      //++pcount[0];_str=s._str ;      //cout<<(*_pcount)<<endl;}char& operator[](int index){if((*_pcount)>1){--_pcount[0];char* Newstr=new char[strlen(_str)+1];int* Newcount=new int[1];strcpy(Newstr,_str);*Newcount=1;_str=Newstr;_pcount=Newcount;}return _str[index];}String& operator=(String& s){if(_pcount[0]==1){cout<<"str3=str1"<<endl;delete[] _str;delete[] _pcount;_str=s._str;_pcount=s._pcount ;++_pcount[0];}else{cout<<"str1=str3"<<endl;_str=s._str;--_pcount[0];_pcount=s._pcount ;++_pcount[0];}return *this;}void PushBack(char c){if(_pcount[0]>1){--_pcount[0];}char* Newstr=new char[strlen(_str)+1+1];    int* Newcount=new int[1];    strcpy(Newstr,_str);*Newcount=1;if(_pcount[0]==1){delete[] _str;delete[] _pcount;}_str=Newstr;    _pcount=Newcount;int n=strlen(_str);_str[n++]=c;_str[n]='\0';}~String(){if(--_pcount[0]==0){cout<<"ss"<<endl;delete[] _pcount;delete[] _str;_pcount=NULL;_str=NULL;}}private:char* _str;int* _pcount;};ostream& operator<<(ostream& os,String& str){os<<str._str;return os; }void test(){String str1("abcdef");String str2(str1);str2.PushBack ('g');str2.PushBack ('h');/*String str3;str1=str3;*/cout<<str2<<endl;str2=str1;String str2(str1);str2[3]='w';cout<<str2<<endl;String str3("abcdef");}

三、用写时拷贝的方法来实现string类

开辟了一块地址,前四个字节用来计数,后面的空间用来存字符串。

class String{friend ostream& operator<<(ostream& os,String& str);public:String(const char* str=""):_str(new char[strlen(str)+1+4]){_str+=4;             //找到存字符的区域Getcount()=1;   strcpy(_str,str);}String(const String& s){_str=s._str;++Getcount();  }char& operator[](int index){if(Getcount()>1){--Getcount();char* Newstr=new char[strlen(_str)+1+4];Newstr+=4;strcpy(Newstr,_str);_str=Newstr;Getcount()=1;}return _str[index];}String& operator=(String& s){if(this!=&s){if(--Getcount()==0){delete[] (_str-4);}++(s.Getcount());_str=s._str;}return *this;}~String(){   cout<<"dd"<<endl;if(--Getcount()==0){cout<<"ss"<<endl;delete[] (_str-4);}}private:int& Getcount(){return*((int *)_str-1);}private:char* _str;};ostream& operator<<(ostream& os,String& str){os<<str._str;return os; }void test(){String str1("abcd");String str2;str2[2]='w';str2=str1;cout<<str2<<endl;}
C++中string类有许多用法,我只实现了其中的一部分,谢谢


0 0
原创粉丝点击