Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / C++自定义String类

String.h头文件:
  1. using namespace std;  
  2. class String{  
  3. public:  
  4.     //默认构造函数   
  5.     String();  
  6.     //带参构造函数   
  7.     String(const char *str);  
  8.     //析构函数   
  9.     ~String();  
  10.     //拷贝构造   
  11.     String(const String &obj);  
  12.     //复制初始化   
  13.     String &operator=(const String &obj);  
  14. public:  
  15.     //把后面字符串加在*this上   
  16.     String &operator+=(const String &obj);  
  17.     //把后面字符串加在前面   
  18.     friend String operator+(const String &obj,const String &obj1);  
  19.     //==判断字符串是否相等   
  20.     bool operator==(const String &str)const;  
  21.     //!=判断字符串是否相等   
  22.     bool operator!=(const String &str)const;  
  23.     //[]下标运算符重载   
  24.     char operator[](int index);  
  25.     //<<运算符重载   
  26.     friend ostream &operator<<(ostream &os,const String &obj);  
  27.     //>>运算符重载   
  28.     friend istream &operator>>(istream &is,const String &obj);      
  29.     //返回字符串长度   
  30.     int leng()const;  
  31.     //取从position所指位置连续取len个字符组成子串返回   
  32.     String& subString(int position,int len);  
  33. private:  
  34.     char *_str;  
  35.     int _len;  
  36. };  
String.cpp实现文件:
  1. #include <iostream>   
  2. //#include <string>   
  3. #include "String.h"   
  4. const int maxLength=128;//字符串的最大长度   
  5. //默认构造函数   
  6. String::String(){  
  7.     _len=0;  
  8.     _str=new char[1];  
  9. }  
  10. //带参构造函数   
  11. String::String(const char *str){  
  12.     _str=new char[maxLength+1];  
  13.     if(!_str)  
  14.     {  
  15.         cerr<<"Allocation Error! ";  
  16.         exit(1);  
  17.     }  
  18.     _len=strlen(str);  
  19.     strcpy(_str,str);  
  20. }  
  21. //析构函数   
  22. String::~String(){  
  23.     delete _str;  
  24.     _str=NULL;  
  25. }  
  26. //拷贝构造   
  27. String::String(const String &obj){  
  28.     _str=new char[maxLength+1];  
  29.     if(!_str)  
  30.     {  
  31.         cerr<<"Allocation Error! ";  
  32.         exit(1);  
  33.     }  
  34.     _len=obj._len;  
  35.     strcpy(_str,obj._str);  
  36. }  
  37. //复制初始化   
  38. String &String::operator=(const String &obj){  
  39.     _str=new char[maxLength+1];  
  40.     if(!_str)  
  41.     {  
  42.         cerr<<"Allocation Error! ";  
  43.         exit(1);  
  44.     }  
  45.     _len=obj._len;  
  46.     strcpy(_str,obj._str);      
  47.     return *this;          
  48. }  
  49. //把后面字符串加在前面   
  50. String &String::operator+=(const String &obj){  
  51.     char *temp=_str;//将*this->ch赋给temp   
  52.     _len=_len+obj._len;//*this->curLen为两字符串的总长度   
  53.     _str=new char[_len+1];  
  54.     if(!_str)  
  55.     {  
  56.         cerr<<"Out of memory! ";  
  57.         exit(1);  
  58.     }  
  59.     strcpy(_str,temp);  
  60.     strcat(_str,obj._str);  
  61.     delete[] temp;  
  62.     return *this;  
  63. }  
  64. //把后面字符串加在前面   
  65. String operator+(const String &obj,const String &obj1){  
  66.     int len=obj._len+obj1._len;//两个长度加起来   
  67.     char *str0=new char[len+1];  
  68.     if(!str0)  
  69.     {  
  70.         cerr<<"Out of memory! ";  
  71.         exit(1);  
  72.     }  
  73.     strcpy(str0,obj._str);  
  74.     strcat(str0,obj1._str);  
  75.     String str(str0);  
  76.     delete[] str0;  
  77.     return str;  
  78. }  
  79. //==判断字符串是否相等   
  80. bool String::operator==(const String &str)const{  
  81.     if(strcmp(_str, str._str)==0){  
  82.         return true;  
  83.     }else{  
  84.         return false;  
  85.     }      
  86. }  
  87. //!=判断字符串是否相等   
  88. bool String::operator!=(const String &str)const{  
  89.     if(strcmp(_str, str._str)!=0){  
  90.         return true;  
  91.     }else{  
  92.         return false;  
  93.     }  
  94. }  
  95. //[]下标运算符重载   
  96. char String::operator[](int index){  
  97.     return _str[index];  
  98. }  
  99. //<<运算符重载   
  100. ostream &operator<<(ostream &os,const String &obj){  
  101.     os<<obj._str;  
  102.     return os;  
  103. }  
  104. //>>运算符重载   
  105. istream &operator>>(istream &is,const String &obj){  
  106.     is>>obj._str;  
  107.     return is;  
  108. }  
  109. //返回字符串长度   
  110. int String::leng()const{  
  111.     return _len;  
  112. }  
  113. //取从position所指位置连续取len个字符组成子串返回   
  114. String& String::subString(int position,int len){  
  115.     if(position<0||position+len-1>=maxLength||len<0) //参数不合理,不取子串   
  116.     {  
  117.         _len=0;  
  118.         _str[0]="";  
  119.     }  
  120.     else  
  121.     {  
  122.         if(position+len-1>=_len)                         //字符串不够长   
  123.             len=_len-position;  
  124.         _len=len;  
  125.         forint i=0,j=position;i<len;i++,j++)  
  126.             _str[i]=_str[j];  
  127.         _str[len]="";  
  128.     }  
  129.     return *this;  
  130. }