首页 > 代码库 > C++String 类完整源代码

C++String 类完整源代码

MyString 类完整源代码

 

MyString.h文件

/***********************************************************

//Copyright (c) 2013 道合|SameIdeal.com  All rights reserved.

************************************************************/

#ifndef MYSTRING_H

#define MYSTRING_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+(const MyString&,const MyString&);

               //比较操作符

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

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

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

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

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

                      friend bool operator>=(const MyString&,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,const MyString&);

               //替换操作

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

               //删除操作

                    MyString& erase(size_t,size_t);

 

private:

                  size_t strLength;

                 char* p_str;

};

 

#endif

 

MyString.cpp文件

/***********************************************************

//Copyright (c) 2013 道合|SameIdeal.com  All rights reserved.

************************************************************/

#include "MyString.h"

#include <cassert>

 

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

 

MyString::MyString(const MyString& str)

{

                    strLength = str.strLength;

                   p_str = new char[strLength+1];

                    strcpy(p_str,str.p_str);

}

 

MyString::MyString(const char* str)

{

                 if(str==NULL)

                               return;

                    strLength = strlen(str);

                   p_str = new char[strLength+1];

                   strcpy(p_str,str);

}

 

MyString::MyString(const size_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_t MyString::length()

{

                  return strLength;

bool MyString::empty()

{

                    return strLength==0?true:false;

const char* MyString::c_str()

{

                 return p_str;

}

//输出操作符

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

{

                   if(str.p_str!=NULL)

                                out<<str.p_str;

                 return out;

}

//输入操作符

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

{

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

                 if(in>>temp)

               {

                                  delete[] str.p_str;

                                   str.strLength = strlen(temp);

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

                                  strcpy(str.p_str,temp);

               }

                 return in;

}

//下标操作符

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

{

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

                  return p_str[index];

}

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

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

{

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

                  return p_str[index];

}

 

//‘+‘操作符的重载

MyString operator+(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);

                 return ret;

}

 

//赋值操作符

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;

}

 

//比较操作符

bool operator==(const MyString& lhs,const MyString& rhs)

{

                       return strcmp(lhs.p_str,rhs.p_str)==0;

}

bool operator!=(const MyString& lhs,const MyString& rhs)

{

                       return strcmp(lhs.p_str,rhs.p_str)!=0;

}

bool operator<(const MyString& lhs,const MyString& rhs)

{

                       return strcmp(lhs.p_str,rhs.p_str)<0;

}

bool operator<=(const MyString& lhs,const MyString& rhs)

{

                       return strcmp(lhs.p_str,rhs.p_str)<=0;

}

bool operator>(const MyString& lhs,const MyString& rhs)

{

                       return strcmp(lhs.p_str,rhs.p_str)>0;

}

bool operator>=(const MyString& lhs,const MyString& rhs)

{

                       return strcmp(lhs.p_str,rhs.p_str)>=0;

}

 

 

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

字符

MyString MyString::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‘;

                  return ret;

}

//添加操作

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

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_t i=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 = new char[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;

}

 

C++String 类完整源代码