String类的写法(引用计数于写时拷贝)

首先推荐大家可以看下陈皓大佬的博客,其实我看了之后是没有勇气写下去的,因为大佬写的实在是太精彩了,下面我分享一下自己的代码。

引用计数

 #include <iostream>
  2 #include <string.h>
  3 #include <assert.h>
  4 class  String
  5 {
  6         public:
  7                 String ()
  8                         :_str(new char[1])
  9                          ,_pCount(new size_t(1))
 10         {
 11                 _str[0]='\0';
 12         }
 13 
 14                 String (char *str)
 15                         :_str(new char[strlen(str)+1])
 16                          ,_pCount(new size_t(1))
 17         {
 18                 strcpy(_str,str);
 19         }
 20 
 21                 String (const  String&  s )
 22                         :_str(s._str)
 23                          ,_pCount(s._pCount)
 24         {
 25                 (*_pCount)++;
 26         }
 27                 ~String ()
 28                 {
 29                         if (--(*_pCount)==0)//当引用计数为零时释放所管理的空间
 30                         {
 31                                 delete[]  _str;
 32                                 delete  _pCount;
 33                         }
 34                 }
 35 
 36 
 37                 const  char*c_str()
 38                 {
 39                         return _str;
 40                 }
 41 
 42         private :
 43                 char*_str;
 44                 size_t*_pCount;//存放引用计数
 45 } ;

写时拷贝+引用计数(多开辟一个int*的指针的空间,来管理引用计数)

  1 #include <iostream>
  2 #include <string.h>
  3 #include <assert.h>
  4 using namespace   std;
  5 
  6 
  7 
  8 class String
  9 {
 10         public:
 11 
 12 
 13                 String ()
 14                         :_str(new char  [1])
 15                          ,_pCount(new int (1))
 16         {
 17                 _str[0]='\0';
 18         }
 19 
 20                 String (const   char *str)
 21                         :_str(new char[strlen(str)+1])
 22                          ,_pCount  (new int(1))
 23         {
 24                 strcpy (_str,str);
 25         }
 26 
 27 
 28                 String (const  String& s)
 29                         :_str(s._str)
 30                          ,_pCount (s._pCount)
 31         {
 32                 (*_pCount)++;
 33         }
 34 
 35 
 36                 String&  operator =(const   String&   s)
 37                 {
 38                         if (_str !=s._str)
 41 
 42                                 _str=s._str;
 43                                 _pCount=s._pCount;
 44                                 (*_pCount)++;
 45                         }
 46                         return *this;
 47                 }
 48 
 49 
 50 
 51                 void Release()
 52                 {
 53                         delete[]  _str;
 54                         delete _pCount;
 55                 }
 56 
 57 
 58 
 59                 ~String ()
 60                 {
 61                         Release();
 62                 }
 63 
 64 
 65 
 66 
 68                 {
 69                         if (*_pCount>1)
 70                         {
 71                                 char *newstr=new char[strlen (_str)+1];
 72                                 strcpy(newstr,_str);
 73                                 --(*_pCount);
 74 
 75                                 _str=new char (1);
 76                                 _pCount=new int (1);
 77                         }
 78                 }
 79                 char&  operator [](size_t  pos )
 80                 {
 81                         assert ((int)pos <strlen (_str));
 82                         CopyOnWrite();
 83 
 84 
 85                         return _str[pos];
 86                 }
 87 
 88 
 89                 const  char*  c_str()
 90                 {
 91                         return _str;
 92                 }
 93 
 94 
 95         private:
 96                 char  *_str;
 97                 int *  _pCount;//管理存放引用计数的空间
 98 };

写时拷贝加引用技术1(在字符串的首位置之前多开辟一块空间,来存放引用计数)

1 #include <iostream>
  2 #include <string.h>
  3 using namespace std;
  4 
  5 
  6 
  7 class String
  8 {
  9         public:
 10                 String ()
 11                         :_str(new char[5])
 12                 {
 13                         _str+=4;
 14                         _str[0]='\0';
 15                         GetRefCount()=1;
 16                 }
 17 
 18 
 19                 String (const String&  s)
 20                         :_str(s._str)
 21                 {
 22                         GetRefCount()++;
 23                 }
 24 
 25 
 26                 String&   operator =(const   String&   s)
 27                 {
 28                         if (_str!=s._str)
 29                         {
 30                                 if (--GetRefCount()==0)
 31                                 {
 32                                         delete [](_str-4);
 33                                 }
 34 
 35                                 _str=s._str;
 36                                 GetRefCount()++;
 37                         }
 38 
 39                         return *this;
 40                 }
 41 
 42 
 43 
 44                 void CopyOnWrite ()
 45                 {
 46                         if (GetRefCount()>1)
 47                         {
 48 
 49 
 50                                 --GetRefCount ();
 51                                 char   *tmp =new char [strlen (_str)+5];
 52                                 tmp+=4;
 53                                 strcpy (tmp,_str);
 54                                 _str=tmp;
 55                                 GetRefCount()=1;
 56                         }
 57                 }
 58 
 59 
 60                 char&  operator [](size_t   pos )
 61                 {
 62 
 63                         CopyOnWrite();
 64                         return _str[pos];
 65                 }
 66 
 67 
 68                 const  char&  operator [] (size_t  pos )const
 69                 {
 70                         return _str[pos];
 71                 }
 72 
 73                 int&   GetRefCount()
 74                 {
 75                         return *(int*)(_str-4);
 76                 }
 77 
 78                 ~String ()
 79                 {
 80                         if (--GetRefCount()==0)
 81                         {
 82                                 delete[]  (_str-4);
 83                         }
 84                 }
 85 
 86 
 87         private:
 88                 char*   _str;
 89 };

猜你喜欢

转载自blog.csdn.net/a15929748502/article/details/81604743