C++集锦四 MyString类

来源:互联网 发布:exe软件看源码 编辑:程序博客网 时间:2024/06/07 00:17

MyString.h文件

#ifndef MYSTRING_H

#defineMYSTRING_H #include <iostream>

using namespace std;

 

class MyString

{

public:

               //构造函数

                 MyString();

                  MyString(const MyString&);

                  MyString(const char*);

                      MyString(const size_t,const char);

               //析构函数

                 ~MyString();

               //属性

                   size_t length();//字符串的长度

                  bool empty();//字符串是否为空

                    const char* c_str();//返回C风格字符串的指针

               //读写操作符

                     friend ostream& operator<<(ostream&,const MyString&);

                     friend istream& operator>>(istream&,MyString&);

                  //‘+’操作符

                       friend MyString operator+(constMyString&,const MyString&);

               //比较操作符

                       friend bool operator==(constMyString&,const MyString&);

                        friend bool operator!=(constMyString&,const MyString&);

                       friend bool operator<(constMyString&,const MyString&);

                       friend bool operator<=(constMyString&,const MyString&);

                       friend bool operator>(constMyString&,const MyString&);

                       friend bool operator>=(constMyString&,const MyString&);

               //下标操作符

                     char& operator[] (const size_t);

                       const char& operator[] (const size_t)const;

               //赋值操作符

                   MyString& operator=(const MyString&);

                //'+='操作符

                   MyString& operator+=(const MyString&);

               //子串操作

                      MyString substr(size_t,size_t);

               //添加操作

                  MyString& append(const MyString&);

               //插入操作

                      MyString& insert(size_t,constMyString&);

               //替换操作

                       MyString& assign(constMyString&,size_t,size_t);

               //删除操作

                     MyString&erase(size_t,size_t);

 

private:

                   size_t strLength;

                 char*p_str;

};

 

#endif

 

MyString.cpp文件

#include"MyString.h"

#include<cassert>

 

MyString::MyString():strLength(0),p_str(NULL){}

 

MyString::MyString(constMyString& str)

{

                     strLength = str.strLength;

                    p_str = new char[strLength+1];

                     strcpy(p_str,str.p_str);

}

 

MyString::MyString(constchar* str)

{

                 if(str==NULL)

                               return;

                     strLength = strlen(str);

                    p_str = new char[strLength+1];

                    strcpy(p_str,str);

}

 

MyString::MyString(constsize_t len,const char ch)

{

                  strLength = len;

       p_str = new char[strLength+1];    *(p_str+strLength)='\0';

                   strset(p_str,ch);

}

 

MyString::~MyString()

{

                  delete[]p_str;

size_tMyString::length()

{

                  returnstrLength;

boolMyString::empty()

{

                     returnstrLength==0?true:false;

const char*MyString::c_str()

{

                  returnp_str;

}

//输出操作符

ostream&operator<< (ostream& out,const MyString& str)

{

                   if(str.p_str!=NULL)

                                 out<<str.p_str;

                 returnout;

}

//输入操作符

istream&operator>>(istream& in,MyString& str)//

{

                 char temp[100];//临时字符串数组

                 if(in>>temp)

               {

                                   delete[] str.p_str;

                                    str.strLength =strlen(temp);

                                      str.p_str = newchar[str.strLength+1];

                                   strcpy(str.p_str,temp);

               }

                 returnin;

}

//下标操作符

char&MyString::operator [](const size_t index)

{

                    assert(index>=0 &&index<=strLength);

                   returnp_str[index];

}

//下标操作符(const情况)

const char&MyString::operator [](const size_t index)const

{

                    assert(index>=0 &&index<=strLength);

                   returnp_str[index];

}

 

//'+'操作符的重载

MyStringoperator+(const MyString& lhs,const MyString& rhs)

{

                 MyString ret;

                         ret.strLength = lhs.strLength +rhs.strLength;

                      ret.p_str = new char[ret.strLength+1];

                      strcpy(ret.p_str,lhs.p_str);

                      strcat(ret.p_str,rhs.p_str);

                 returnret;

}

 

//赋值操作符

MyString&MyString::operator=(const MyString& str)

{

                   if(this!=&str)

               {

                                    if(strLength<str.strLength)

                              {

                                                delete[]p_str;

                                                   p_str= new char[str.strLength+1];

                              }

                                    strLength = str.strLength;

                                    strcpy(p_str,str.p_str);

               }

                  return*this;

}

 

//'+='操作符

MyString&MyString::operator+=(const MyString& str)

{

                   if(this == &str)

               {

                                 MyString copy(str);

                                 return *this+=copy;

               }

                     strLength += str.strLength;

                   char* p_old = p_str;

                    p_str = new char[strLength+1];

                   strcpy(p_str,p_old);

                  delete[] p_old;

                      strcat(p_str,str.p_str);

                  return*this;

}

 

//比较操作符

booloperator==(const MyString& lhs,const MyString& rhs)

{

                       returnstrcmp(lhs.p_str,rhs.p_str)==0;

}

booloperator!=(const MyString& lhs,const MyString& rhs)

{

                       returnstrcmp(lhs.p_str,rhs.p_str)!=0;

}

booloperator<(const MyString& lhs,const MyString& rhs)

{

                       returnstrcmp(lhs.p_str,rhs.p_str)<0;

}

booloperator<=(const MyString& lhs,const MyString& rhs)

{

                       returnstrcmp(lhs.p_str,rhs.p_str)<=0;

}

booloperator>(const MyString& lhs,const MyString& rhs)

{

                       returnstrcmp(lhs.p_str,rhs.p_str)>0;

}

booloperator>=(const MyString& lhs,const MyString& rhs)

{

                       returnstrcmp(lhs.p_str,rhs.p_str)>=0;

}

 

 

//子串操作 //返回一个MyString类型的字符串,它包含源字符串中从下标pos开始的n个

字符

MyStringMyString::substr(size_t pos,size_t n)

{

       assert(pos+n<=strLength);     MyString ret;

                   ret.strLength = n;

                      ret.p_str = new char[ret.strLength+1];

                      for (size_t i=0;i!=n;++i)

                                   ret[i]=(*this)[pos+i];

                  ret[n]='\0';

                 returnret;

}

//添加操作

//将一个字符串的副本添加到源字符串的末尾,同“+=”操作符类似

MyString&MyString::append(const MyString& str)

{

                 *this+=str;

                  return*this;

}

//插入操作

//在下标为pos的元素之前插入MyString对象str的副本

MyString&MyString::insert(size_t pos,const MyString& str)

{

                   assert(pos<strLength);

                   char* p_old = p_str;

                    strLength +=str.strLength;

                    p_str = new char[strLength+1];

                      for (size_t i=0;i!=pos;++i)

                                   *(p_str+i) = *(p_old+i);

       for(size_t i=pos;i!=str.strLength+pos;++i)          *(p_str+i)= *(str.p_str+i-pos);

                          for(size_ti=str.strLength+pos;i!=strLength;++i)

                                      *(p_str+i) =*(p_old+i-str.strLength);

                    *(p_str+strLength)='\0';

                  delete[] p_old;

                  return*this;

}

//替换操作

//用s2中从下标pos开始的len个字符副本替换源字符串

MyString&MyString::assign(const MyString& s2,size_t pos,size_t len)

{

                  if(strLength<len)

               {

                                 strLength = len;

                                 delete[] p_str;

                                   p_str = newchar[strLength+1];

               }

                      for(size_t i=0;i!=len;++i)

                                  *(p_str+i)=s2[pos+i];

                    *(p_str+strLength)='\0';

                  return*this;

}

//删除操作

//删除从下标pos开始的len个字符

MyString&MyString::erase(size_t pos,size_t len)

{

                   assert(pos+len<=strLength);

                     size_t index = pos + len;

                    while(*(p_str+index)!='\0')

               {

                                    *(p_str+index-len)=*(p_str+index);

                               ++index;

               }

                    *(p_str+index-len)='\0';

                  return*this;

}

原创粉丝点击