c++ String 类的简单实现和写时拷贝

来源:互联网 发布:远程网络教育的优势 编辑:程序博客网 时间:2024/06/05 07:10
#define _CRT_SECURE_NO_WARNINGS 1#include<iostream>#include<string>using namespace std;class String {public:    friend ostream& operator<<(ostream& os, String& str) ;     String()    : _str(new char[1])  {    *_str = '\0';  }    String(const char* str)        :_str(new char[strlen(str)+1])    {        strcpy (_str,str);    }     String(const String& str)         :_str(new char[strlen(str._str )+1])     {         strcpy(_str ,str._str );     }    /* String& operator = (const String& str)         {         if(this != &str)         {               delete[] _str;             _str= new char[strlen(str._str )+1];            strcpy(_str, str._str );         }         return *this;     }*/     String& operator = ( String str)     {         swap(_str , str._str );         return *this;     }    ~String()    {        if(_str != NULL)        {            delete[] _str;        }    }      const char *c_str()      {          return _str;      } public:      char& operator[](int  index)      {          return _str[index];      }      String& operator+=(const String& s)      {           int sz = strlen(_str) + strlen(s._str )+1;           Check( sz);           strcat(_str,s._str );           return *this;      }      String& Insert(int pos, const String& s)  //《就这个函数想的我头疼,终于也是做出来了,嘎嘎》      {          int sz = strlen(_str)+strlen(s._str)+1;          Check(sz);          for(int i = strlen(_str) ; i >= pos;i--)//先将 某位置起所需的空间 的后面的元素进行后移所需空间的个数          {              _str[i+strlen(s._str )] = _str[i];          }          for(int j = pos, k = 0; k < strlen(s._str ),j < pos+strlen(s._str );k++,j++) //从某位置起插入所需的字符串          {              _str[j] = s._str [k];          }          return *this;      }      void PushBack(char ch)      {         int sz = strlen(_str)+2;         Check( sz);         _str[sz-2] = ch;         _str[sz-1] ='\0';      }      void Check(int sz)      {          char* tmp = new char[sz];          strcpy(tmp,_str);          delete[] _str;          _str = tmp;      }private:    char* _str;};  ostream& operator<<(ostream& os, String& str)  {      os << str._str ;      return os;  }  void test()  {    String str1("hello world !");    String str2(str1);    String str3;    str3 = str2;    String str4("change world!");    str1+=str4;    cout<<str1[2]<<endl;    cout<<str1<<endl;    cout<<str2<<endl;    cout<<str3<<endl;    cout<<str1<<endl;    cout<<str4[2]<<endl;  }  void test3()  {      String str1("hello worl");      str1.PushBack ('d');      cout<<str1<<endl;  }  void test2()  {      String str1("horld !");      cout<<str1.Insert(1,"ello w")<<endl;  }int main(){    test2();    return 0;}写时拷贝(更好的开辟空间的方式是一次性开辟string的空间和_pcount的空间。然后利用指针的偏移进行数据的操作,此处的方法类似,只不过多用了一次new,优点在于方便理解,容易查找出错误)#include<iostream>#include<string>using namespace std;class String {public:    friend ostream& operator<<(ostream& os, String& str) ;     String()    : _str(new char[1])    ,_pcount(new int(1))  {      *_str = '\0';  }    String(const char* str)        :_str(new char[strlen(str)+1])        ,_pcount(new int(1))    {        strcpy (_str,str);    }     String(const String& str)         :_str(str._str )         ,_pcount(new int(1))     {             delete _pcount;         _pcount = str._pcount ;         (*_pcount)++;     }     String& operator = ( String& str)        {         if(this != &str)         {               delete _pcount;              delete[] _str;              _str= str._str ;             _pcount = str._pcount ;             (*_pcount)++;         }         return *this;     }    ~String()    {        if(--(*_pcount) == 0 )        {            delete[] _str;            delete _pcount;        }        cout<<" ~String()"<<endl;    }private:    char* _str;    int* _pcount;};  ostream& operator<<(ostream& os, String& str)  {      os << str._str ;      return os;  }  void test()  {    String str1("hello world !");    String str2(str1);    String str3;    str3 = str2;    cout<<str1<<endl;    cout<<str2<<endl;    cout<<str3<<endl;  }int main(){    test();    return 0;}
0 0
原创粉丝点击