Imitating and implementing C ++ library functions ---- String class ---- Using write-time copy implementation
# Define _ CRT_SECURE_NO_WARNINGS 1 # include <iostream> # include <assert. h ># define DEFAULTNUME 3 using namespace std; class String {public: int & GetCount () {return * (int *) _ str)-1 );} char * GetStr () {return _ str;} // locate the ch character and return its subscript int Find (char ch) {if (_ str = NULL) {return-1;} else {int count = 0; char * str = _ str; while (str & count <_ size) {if (* str = ch) {return count;} str ++; count ++ ;}} return-1 ;}// implements subscript access to char & Operator [] (int pos) {char * cur = _ str; cur = cur + pos; return * cur;} bool operator <(const String & s) {char * pSourse = this-> _ str; char * pDest = s. _ str; if (pSourse = NULL) {return false;} else if (pDest = NULL) {return true;} else {while (* pSourse! = NULL) & (pDest! = NULL) {if (* pSourse <* pDest) {return true;} else if (* pSourse> * pDest) {return false;} else {pSourse ++; pDest ++ }}} if (* pSourse = NULL & * pDest! = NULL) {return true;} else {return false;} bool operator = (const String & s) {char * pSource = _ str; char * pDest = s. _ str; if (_ size = s. _ size) {while (* pSource = * pDest & * pSource! = NULL & * pDest! = NULL) {pSource ++; pDest ++;} if (* pSource = NULL & * pDest = NULL) {return true ;}} return false ;} bool operator> (const String & s) {return! (* This <s) & (* this = s);} bool operator >=( const String & s) {return (* this> s) & (* this = s);} bool operator <= (const String & s) {return (* this <s) & (* this = s);} bool operator! = (Const String & s) {return! (* This = s);} char * CheckCapacity (int capacity) {if (capacity> _ capacity) {char * cur = new char [capacity + defanume]; return cur;} return _ str; // >>>>>>>>>>>>>>>>>>>>>> modify} void Insert (int pos, const char * s) {* (int *) _ str)-1)-= 1; int len = strlen (s ); char * p = CheckCapacity (_ size + len + 1); strcpy (p, _ str); _ str = p; p = NULL; // move the empty space of the original string to the int moveCount = _ size-pos + len + 1; // moveCount indicates the number of moves Int I = _ size; // I is the moving element position while (moveCount --) // move the position of the pos and the characters following the pos group to the back one {_ str [I + len] = _ str [I]; I --;} // Insert the string to be inserted // int insertCount = len; // insertCount indicates the number of characters inserted // int posSource = pos; // posSource is the subscript of the original string // int posDest = 0; // posDest is the subscript of the string s to be inserted // while (insertCount --) // {// _ str [posSource-1] = s [posDest]; // posSource ++; // posDest ++; ///} // >>> use strncpy to implement the function. (pay attention to common library functions !) Char * tem = _ str + pos-1; strncpy (tem, s, len); _ size + = len; _ capacity = _ size + 4; // 4 is '\ 0' + DEFAULTNUME number} void Insert (size_t pos, const char ch) {* (int *) _ str)-1) -= 1; char * p = new char [_ capacity + 1]; strcpy (p, _ str); _ str = p; p = NULL; int count = _ size-pos + 2; // count indicates the number of moves int I = _ size; // the position of the moving element while (count --) // move the position of the pos and the character group after the pos to the back one {_ str [I + 1] = _ str [I]; I --;} _ str [pos-1] = ch; _ s Ize ++; _ capacity = _ size + 4; // 4 is '\ 0' + defanumnume number} String & operator + = (const String & s) {* (int *) _ str)-1)-= 1; char * cur = CheckCapacity (_ size + s. _ size + 5); cur = cur + 4; strcpy (cur, _ str); // copy the original string _ str = cur; Insert (_ size + 1, s. _ str); // copy s. _ str string * (int *) cur-1) = 1; cur = NULL; _ size + = s. _ size; _ capacity = _ size + 4; // 4 is the number of '\ 0' + defanumnume return * this;} public: String (char * str = ""): _ Str (new char [_ size + 5]) {_ str = _ str + 4; strcpy (_ str, str); _ size = strlen (str ); _ capacity = _ size + 1; // GetCount () = 1; // Why is the error? * (Int *) _ str)-1) + = 1; // usage ++ why is the error} String (const String & s) {_ str = s. _ str; _ size = s. _ size; _ capacity = _ size + 1; int count = GetCount (); GetCount () ++ ;}~ String () {if (-- GetCount () = 0 & _ str! = NULL) {delete [] _ str; _ str = NULL; // >>>>>>>>>>>>>>>>>>>>change} String & operator = (const String & s) {if (this! = & S) {_ str = s. _ str; _ size = s. _ size; _ capacity = _ size + 1; int count = GetCount (); // GetCount () ++; * (int *) _ str)-1) + = 1;} return * this;} private: void Check (int a); // implement reference count char * _ str; int _ size; int _ capacity ;}; // test constructor copy constructor destructor value assignment operator reload reference count void test1 () {String s1 ("abcd"); cout <s1.GetStr () <endl; string s2 = s1; cout <s2.GetStr () <endl; String s3; cout <s3.GetStr () <endl; s3 = s1; cout <s 3. getStr () <endl;} // test Find operator [] (read-only operation) void test2 () {String s1 ("abcdefg "); cout <s1.Find ('B') <endl; cout <s1.Find ('G') <endl; cout <s1.Find ('H') <endl; cout <s1 [2] <endl; cout <s1 [6] <endl; cout <s1 [9] <endl ;} // test operator <operator = operator> // test operator <= operator> = operator! = Void test3 () {String s1 ("abcd"); String s2 ("abc"); String s3 ("abcd"); String s4 ("acd "); cout <(s2 <s1) <endl; cout <(s1 <s2) <endl; cout <(s1 <s3) <endl; cout <(s1 <s4) <endl; cout <(s1 = s3) <endl; cout <(s1 = s2) <endl ;} // test operator + = Insert void test4 () {String s1 ("tan"); String s2 ("xiaohui is superman"); cout <(s1 + = s2 ). getStr () <endl; // s1.Insert (2, "ooooo"); // cout <s1.GetStr () <endl; s1.Insert (4 ,'-'); cout <s1.GetStr () <endl ;}int main () {// test1 (); // test2 (); // test3 (); test4 (); system ("pause"); return 0 ;}