cstring 的重载

时间:2023-03-09 21:39:47
cstring 的重载
 #include <iostream>
#include <windows.h> using namespace std; const int Max = +;
class MyString
{
public: MyString()
{
this -> m_pchData = new char[Max];
this -> Length = ; }
void Init(char* a1) // 初识化
{
int l = strlen(a1);
this->Length = l;
memcpy(this->m_pchData,a1,l);
} int GetLength() const
{
return this->Length;
} bool IsEmpty()
{
if (this->Length==)
return true;
else return false;
} char GetAt(int nIndex) const
{
return this->m_pchData[nIndex-];
}
void Empty();
MyString(const MyString& a1);
char operator[](int nIndex) const;
void SetAt(int nIndex, char a1)
{
if (nIndex>=this->Length||nIndex<)
{
cout << "输入位置有误" << endl;
}
else
{
this->m_pchData[nIndex-] = a1;
} }
char* GetString ()
{
return this->m_pchData;
}
int GetLength()
{
return this->Length;
} const char* operator=(const char* stringSrc)
{
int l = strlen(stringSrc);
this->Length = l;
memset(this->m_pchData,,sizeof(this->m_pchData));
memcpy(this->m_pchData,stringSrc,l);
}
const char operator=(char a1)
{
this->Length = ;
memset(this->m_pchData,,sizeof(this->m_pchData));
this->m_pchData[] = a1; }
const char* operator+=(const char* string)
{
int l = strlen(string) ;
for (int i = ;i<l;i++)
{
this->m_pchData[this->Length+i] = string[i];
}
this->Length += l; }
const char* operator+=(char a1)
{ this->m_pchData[this->Length] = a1;
this->Length++;
}
int Replace(char lpszOld, char lpszNew)
{
for (int i = ;i<this->Length;i++)
{
if (this->m_pchData[i]==lpszOld)
{
this->m_pchData[i] = lpszNew;
}
}
}
int Remove(char chRemove);
int Insert(int nIndex, char ch);
char* GetBuffer(int Index)
{
if (Index >= this->Length||Index<)
{
cout << "输入有误" << endl;
}
else
{
int iNum = ;
int b = this->Length-Index+;
cout << b << endl;
char* a = new char[this->Length-Index+];
for (int i = Index-;i<this->Length;i++)
{
a[iNum++] = this->m_pchData[i];
} return a;
} }
private: char* m_pchData;
int Length; };
VOID operator<<(ostream& os,MyString& a1)
{
cout << a1.GetLength() << endl;
cout << a1.GetString() << endl;
} int main()
{
MyString mystring;
char a[] = "Hello Worldddddddddddddddddddd";
mystring.Init(a);
//cout << mystring.GetLength() << endl;
/* if (mystring.IsEmpty())
{
cout << "Empty" << endl;
}
else
{
cout << "Not Empty" << endl;
} cout << mystring.GetAt(3) << endl; mystring.SetAt(3,'a');
cout << mystring; char a1[20] = "gnajhg";
mystring += a1;
cout << mystring ;
*/ cout << mystring.GetBuffer() << endl;
cout << mystring ; return ;
}