自行设计的一个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;
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值