cstring 的重载

  1 #include <iostream>
  2 #include <windows.h>
  3 
  4 using namespace std;
  5 
  6 
  7 
  8 
  9 const int Max = 100000 +10;
 10  class MyString
 11 {
 12 public:
 13      
 14 
 15     MyString()
 16     {
 17         this -> m_pchData = new char[Max];
 18         this -> Length = 0;
 19          
 20     }
 21     void Init(char* a1)  // 初识化 
 22     {
 23         int l = strlen(a1);
 24         this->Length = l;
 25         memcpy(this->m_pchData,a1,l);
 26     }
 27 
 28     int GetLength() const
 29     {
 30         return  this->Length;
 31     } 
 32      
 33     bool IsEmpty()
 34     {
 35         if (this->Length==0)
 36         return true;
 37         else return false;
 38     }
 39      
 40     char GetAt(int nIndex) const
 41     {
 42         return this->m_pchData[nIndex-1];
 43     }
 44     void Empty();
 45     MyString(const MyString& a1);
 46     char operator[](int nIndex) const;
 47     void SetAt(int nIndex, char a1)
 48     {
 49         if (nIndex>=this->Length||nIndex<0)
 50         {
 51             cout << "输入位置有误" << endl; 
 52         }
 53         else 
 54         {
 55             this->m_pchData[nIndex-1] = a1;
 56         }
 57          
 58     }
 59     char* GetString ()
 60     {
 61         return this->m_pchData;
 62     }
 63     int GetLength()
 64     {
 65         return this->Length;
 66     }
 67   
 68  
 69     const char* operator=(const char* stringSrc)
 70     {
 71         int l = strlen(stringSrc);
 72         this->Length = l;
 73         memset(this->m_pchData,0,sizeof(this->m_pchData));
 74         memcpy(this->m_pchData,stringSrc,l);
 75     }
 76     const char operator=(char a1)
 77     {
 78         this->Length = 1;
 79         memset(this->m_pchData,0,sizeof(this->m_pchData));
 80         this->m_pchData[0] = a1;
 81         
 82     }
 83    const char* operator+=(const char* string)
 84    {
 85            int l = strlen(string) ;
 86         for (int i = 0;i<l;i++)
 87         {
 88             this->m_pchData[this->Length+i] = string[i];
 89         }
 90         this->Length += l;
 91          
 92    }
 93     const char* operator+=(char a1)
 94     {
 95      
 96         this->m_pchData[this->Length] = a1;
 97         this->Length++;
 98     }
 99     int Replace(char lpszOld, char lpszNew)
100     {
101         for (int i = 0;i<this->Length;i++)
102         {
103             if (this->m_pchData[i]==lpszOld)
104             {
105                 this->m_pchData[i] = lpszNew;
106             }
107         }
108     }
109     int Remove(char chRemove);
110     int Insert(int nIndex, char ch);
111     char* GetBuffer(int Index)
112     {
113         if (Index >= this->Length||Index<0)
114         {
115             cout << "输入有误" << endl;
116         }
117         else 
118         {
119             int iNum = 0;
120             int b = this->Length-Index+1;
121             cout << b << endl;
122             char* a = new char[this->Length-Index+1];
123             for (int i = Index-1;i<this->Length;i++)
124             {
125                 a[iNum++] = this->m_pchData[i];
126             }
127 
128             return a;
129         }
130          
131     }
132 private:
133        
134      char* m_pchData;
135     int   Length;
136 
137 };
138 VOID operator<<(ostream& os,MyString& a1)
139 {
140     cout << a1.GetLength() << endl;
141     cout << a1.GetString() << endl;
142 }
143  
144 int main()
145 {
146     MyString mystring;
147     char a[200] = "Hello Worldddddddddddddddddddd";
148     mystring.Init(a);
149     //cout << mystring.GetLength() << endl;
150 /*    if (mystring.IsEmpty())
151     {
152         cout << "Empty" << endl;
153     }
154     else 
155     {
156         cout << "Not Empty" << endl;
157     }
158 
159     cout << mystring.GetAt(3) << endl;
160      
161     mystring.SetAt(3,'a');
162     cout << mystring;
163 
164      
165     char a1[20] = "gnajhg";
166     mystring += a1;
167     cout << mystring ;
168 */
169 
170     cout << mystring.GetBuffer(6) << endl;
171     cout << mystring ;
172     
173 
174     return 0;
175 }
View Code

 

posted on 2015-06-09 20:58  yifi  阅读(274)  评论(0编辑  收藏  举报

导航