标签:
class String { friend ostream &operator<<(ostream &os,String &s); public: String(const char *str=""); //全缺省的构造函数,解决空字符串的问题 String(const String &ps); //深拷贝 String &operator=(String s); String &operator+=(const char * s); const char *C_Str()const //得到C风格的字符指针 { return _pstr; } char &operator[](size_t index) { return _pstr[index]; } size_t Size()const { return _size; } void PushBack(char c); String &Insert(size_t pos,const char *str); //String &operator=(String &s) //{ // cout<<"String &operator=(String &s)"<<endl; // if(this != &s) // { // delete[]_pstr; // _pstr=new char[strlen(s._pstr)+1]; // strcpy(_pstr,s._pstr); // } // return *this; //} ~String() { cout<<"~String()"<<endl; if(_pstr != NULL) { delete[]_pstr; _pstr=NULL; _size=0; _capacity=0; } } private: void CheckCapacity(int count); private: int _size; int _capacity; char *_pstr; }; ostream &operator<<(ostream &os,String &s) { os<<s._pstr; return os; } String::String(const char *str) :_size(strlen(str)) ,_capacity(strlen(str)+1) ,_pstr(new char[_capacity]) { cout<<"String()"<<endl; strcpy(_pstr,str); } String::String(const String &ps) :_size(ps._size) ,_capacity(strlen(ps._pstr)+1) ,_pstr(new char[_capacity]) { cout<<"String(const String &ps)"<<endl; strcpy(_pstr,ps._pstr); } String &String::operator=(String s) { cout<<"String &operator=(String s)"<<endl; std::swap(_pstr,s._pstr); std::swap(_size,s._size); std::swap(_capacity,s._capacity); return *this; } void String::CheckCapacity(int count) { if(_size+count >= _capacity) { int _count=(2*_capacity)>(_capacity+count)?(2*_capacity):(_capacity+count); char *tmp=new char[_count]; strcpy(tmp,_pstr); delete[]_pstr; _pstr=tmp; _capacity=_count; } } void String::PushBack(char c) { CheckCapacity(1); _pstr[_size++]=c; _pstr[_size]='\0'; } String &String::operator+=(const char * s) { CheckCapacity(strlen(s)); while(*s) { _pstr[_size++]=*s; s++; } _pstr[_size]='\0'; return *this; } String &String::Insert(size_t pos,const char *str) { char *tmp=new char[strlen(_pstr+pos)]; strcpy(tmp,_pstr+pos); CheckCapacity(strlen(str)); while(*str) { _pstr[pos++]=*str; str++; } strcpy(_pstr+pos,tmp); return *this; }
void text1() { String str1("hello"); String str2(str1); String str3; str3=str1; cout<<str1<<endl; cout<<str2<<endl; cout<<str3<<endl; cout<<strlen(str1.C_Str())<<endl; //5 str1[4]='w'; cout<<str1<<endl; //hellw } void text2() { String str1("abcd"); cout<<str1<<endl; str1.PushBack('e'); str1.PushBack('f'); str1.PushBack('g'); str1.PushBack('h'); str1.PushBack('i'); cout<<str1<<endl; cout<<str1.Size()<<endl; } void text3() { String str1("hello"); String str2("hello world"); String str3(str2); str1+=" "; str1+="world"; cout<<str1<<endl; str2.Insert(6," abc "); cout<<str2<<endl; }
//写时拷贝的方式 class String { friend ostream& operator<<(ostream & os,String &s); public: String(const char *str="") :_str(new char[strlen(str)+1+4]) { cout<<"构造"<<endl; _str+=4; *((int *)(_str-4))=1; strcpy(_str,str); } String(String &s) { cout<<"拷贝构造"<<endl; ++*((int *)(s._str-4)); _str=s._str; } String &operator=(const String &s) { cout<<"赋值语句"<<endl; if(--*(int *)(_str-4) == 0) { delete[](_str-4); } ++(*(int *)(s._str-4)); _str=s._str; return *this; } char &operator[](int index) //写时拷贝 { assert(index >= 0 && index < (int)strlen(_str)); if(*(int *)(_str-4) > 1) { --*(int *)(_str-4); char *tmp=new char[strlen(_str)+5]; strcpy(tmp+4,_str); delete[](_str-4); _str=tmp+4; *(int *)(_str-4)=1; } return _str[index]; } ~String() { cout<<"析构"<<endl; if(--*(int *)(_str-4) == 0) { cout<<"释放"<<endl; delete[](_str-4); } } private: char *_str; }; ostream& operator<<(ostream &os,String &s) { os<<s._str; return os; }
void test1() { String str1("abcd"); cout<<str1<<endl; String str2(str1); cout<<str2<<endl; String str3; str3=str1; cout<<str3<<endl; } void test2() { String str1("abcd"); cout<<str1<<endl; String str2; str2=str1; cout<<str2<<endl; str2[2]='w'; cout<<str2<<endl; }
标签:
原文地址:http://blog.csdn.net/qq_34328833/article/details/52266118