String
在C++的學習生涯我中發現String類的功能十分強大,所以我們是很有必要模擬實現它的,況且在面試的時候模擬實現一個String類也是面試官經常會考的,但是因為外界因素的限制我們是不可能模擬的和庫里的string一致的(C++庫里的string功能更強大),所以今天我們只模擬實現string的基本功能-構造函數,拷貝構造函數,析構函數,賦值運算符重載,運算符+=的重載,運算符[]的重載,c_str(得到一個C風格的字符指針,可操作字符串),Size,Push_Back,Insert(深拷貝),以及用寫時拷貝copy_on_write的方式實現基本的String類
深拷貝的方式
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; }
通過測試上述代碼可正常運行,特別是在實現賦值運算符重載的時候我們使用了兩種方式,值得一提的是應用swap函數來實現賦值運算符的重載(在傳參時不可以傳引用),因為應用swap函數實現是根據臨時變量的創建并且該臨時變量出作用域就會自動調用析構函數銷毀(現代的方法)
測試深拷貝的方法
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; }
實現了深拷貝的方法那仫有沒有更加高效的方法呢?當然,那就是寫時拷貝,我們發現在上述深拷貝的版本里實現的拷貝構造函數又為新的對象重新開辟空間(防止淺拷貝的后遺癥:淺拷貝是值拷貝使得兩個指針指向同一塊空間,在析構該空間時對同一塊空間釋放多次就會出現問題),那仫如果我們繼承了淺拷貝的后遺癥-就讓多個指針指向同一塊空間,此時我們只需要設置一個指針變量讓它記錄指向這塊空間的指針個數,在析構時只要該指針變量的內容為1我們就釋放這塊空間否則就讓計數器減1,這就是寫時拷貝的主要思想,下面就讓我們用寫時拷貝的方法實現一個簡單的String類吧
寫時拷貝的方法
//寫時拷貝的方式 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; }
以上所述是小編給大家介紹的C++中sting類的簡單實現方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!
新聞熱點
疑難解答