自行設計的一個ustring類

半夜醒來,閒着沒事,自己設計了下string類,貼到BLOG上。C++都學迷茫了,好久也沒動了,今以此立照,激勵下學習。當然啊,自己的C++底子薄的要命,其中難免錯漏百出,因而貼帖更期望的是得到老人指點些啊。

class ustring
{
private:
 char *pstr;
 int length;
public:
 ustring();
 ustring(const char *str);
 ustring(const ustring &str);//注意,這裏一定要用引用作參數

 void operator=(const char* str);
 void operator=(const ustring& str);
 void operator+=(const char* str);
 void operator+=(const ustring str);
 void CopyStr(const char* str);
 void CopyStr(const ustring& str);

 int StrLength() const;
 void Empty();
 int  isEmpty() const;
 void display() const;

 ustring operator+(const char* str) const;//不能返回引用哦
 ustring operator+(const ustring& str) const;
 void StrCat(const char* str);
 void StrCat(const ustring& str);

 char GetChar(int index) const;
 char operator[](int index) const;

 int operator>(const char*str) const;
 int operator>(const ustring &str) const;
 int operator<(const char*str) const;
 int operator<(const ustring &str) const;
 int operator==(const char*str) const;
 int operator==(const ustring &str) const;
 int StrCmpare(const char* str) const;
 int StrCmpare(const ustring &str) const;

 int Find(const char* str) const;
 int Find(const ustring& str) const;

 char*& getStr();//
 char* getStr(char*str,int n) const;

 ~ustring();
};


 ustring::ustring():pstr(new char('/0')),length(0){}
 ustring::ustring(const char *str)
 {
  length=strlen(str);
  pstr=new char[length+1];
  strcpy(pstr,str);
  
 }
 ustring::ustring(const ustring &str)//注意,這裏一定要用引用作參數
 {
  length=str.length;
  pstr=new char[length+1];
  strcpy(pstr,str.pstr);
 }

 ustring::~ustring()
 {
  if(NULL!=pstr)
   delete[] pstr;
 }

 void ustring::operator=(const char* str)
 {
  if(NULL!=pstr)
   delete[] pstr;//刪除原來的pstr串內存空間
  length=strlen(str);
  pstr=new char[length+1]; //分配新的內存空間
  strcpy(pstr,str);
 }
 void ustring::operator=(const ustring& str)
 {
  (*this)=str.pstr;
 }
 void ustring::operator+=(const char* str)
 {
  *this=(*this)+str;
 }
 void ustring::operator+=(const ustring str)
 {
  *this=(*this)+str;
 }
 void ustring::CopyStr(const char* str)
 {
  (*this)=str;
 }
 void ustring::CopyStr(const ustring& str)
 {
  (*this)=str;
 }
 int ustring::StrLength() const
 {
  return length;
 }
 void ustring::Empty()
 {
  length=0;pstr[0]='/0';
 }
 int  ustring::isEmpty() const
 {
  if(0==length)
   return 1;
  else
   return 0;
 }
 void ustring::display() const
 {
  cout<<pstr<<endl;
 }
 ustring ustring::operator+(const char* str) const//不能返回引用哦
 {
  ustring temp;
  temp.length=length+strlen(str);
  temp.pstr=new char[temp.length+1];
  strcpy(temp.pstr,pstr);
  strcat(temp.pstr,str);
  return temp;
 }
 ustring ustring::operator+(const ustring& str) const
 {
  return (*this)+str.pstr;
 }
 void ustring::StrCat(const char* str)
 {
  *this=(*this)+str;
 }
 void ustring::StrCat(const ustring& str)
 {
  *this=(*this)+str;
 }

 char ustring::GetChar(int index) const
 {
  if(index<length && index>=0)
   return pstr[index];
  else
   return -1;
 }
 char ustring::operator[](int index) const
 {
  return GetChar(index);
 }

 int ustring::operator>(const char*str) const
 {
  if( strcmp(pstr,str)>0 ) return 1;
  else return 0;
 }
 int ustring::operator>(const ustring &str) const
 {
  return (*this)>str.pstr;
 }
 int ustring::operator<(const char*str) const
 {
  if( strcmp(pstr,str)<0 ) return 1;
  else return 0;
 }
 int ustring::operator<(const ustring &str) const
 {
  return (*this)<str.pstr;
 }
 int ustring::operator==(const char*str) const
 {
  if( strcmp(pstr,str)==0 ) return 1;
  else return 0;
 }
 int ustring::operator==(const ustring &str) const
 {
  return (*this)==str.pstr;
 }
 int ustring::StrCmpare(const char* str) const
 {
  return strcmp(pstr,str);
 }
 int ustring::StrCmpare(const ustring &str) const
 {
  return strcmp(pstr,str.pstr);
 }

 int ustring::Find(const char* str) const
 {
  int i,j,index,subStrlen;
 
  subStrlen=strlen(str);
  for(index=0;index<length-subStrlen+1;index++)
  {
   for(i=index,j=0;j<subStrlen;i++,j++)
   {
    if(pstr[i]!=str[j]) break;
   }
   if(j==subStrlen)
    return index;

  }
  return -1;
 }
 int ustring::Find(const ustring& str) const
 {
  return Find(str.pstr); 
 }

 char*& ustring::getStr()//這裏這樣的操作很危險,使用不當會引起類外釋放pstr內存導致程序崩潰
 {
  return pstr;
 }

 char* ustring::getStr(char*str,int n) const
 {
  int i;
  if(!str)
   return NULL;
  for(i=0;i<n-1 && (pstr[i]!='/0');i++)
   str[i]=pstr[i];
  str[i]='/0';
  return str;
 }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章