实现精简版string类

c语言利用数组保存字符串,经常在不经意中浪费了大量的空间,使用起来十分的不方便,而且容易出错,为了解决c语言字符串的问题,c++增加了一个string类。本例就是为了说明通过运算符的重载而实现的。

代码如下:


[cpp]  view plain copy
  1. #include<iostream>  
  2. using namespace std;  
  3. class String  
  4. {  
  5. public:  
  6.     String();  
  7.     ~String();  
  8.     String(const String &rs);  
  9.     String(const charconst ch);  
  10.     char &operator[](unsigned short int length );  
  11.     char operator[](unsigned short int  length) const;  
  12.   
  13.     String &operator=(const String &s);  
  14.     String operator+(const String &);  
  15.     void operator+=(const String &rs);  
  16.   
  17.     friend ostream &operator<<(ostream & o,const String &str)  
  18.     {  
  19.         o<<str.str;  
  20.         return o;  
  21.       
  22.           
  23.       
  24.     }  
  25.   
  26.     friend istream &operator>>(istream &i,String str)  
  27.     {  
  28.       
  29.         i>>str.str;  
  30.         return i;  
  31.     }  
  32.   
  33.     friend bool operator<(const String &str1,const String &str2)  
  34.     {  
  35.       
  36.         if(strcmp(str1.str,str2.str)<0)  
  37.             return 1;  
  38.         else   
  39.             return 0;  
  40.     }  
  41.   
  42.     friend bool operator>(const String &str1,const String &str2)  
  43.     {  
  44.       
  45.         if(strcmp(str1.str,str2.str)>0)  
  46.             return 1;  
  47.         else   
  48.             return 0;  
  49.     }  
  50.   
  51.         friend bool operator==(const String &str1,const String &str2)  
  52.     {  
  53.       
  54.         if(strcmp(str1.str,str2.str)==0)  
  55.             return 1;  
  56.         else   
  57.             return 0;  
  58.     }  
  59.   
  60.         unsigned short int getlen() const {return len;}  
  61.         const char* getstr() const{return str;}  
  62.   
  63.   
  64.   
  65. private:  
  66.     String(unsigned short int);  
  67.     unsigned short int len;  
  68.     char *str;  
  69.   
  70.   
  71.   
  72.   
  73. };  
  74.   
  75. String::String(unsigned short int length)  
  76. {  
  77.   
  78.     str = new char[length + 1];  
  79.     int i;  
  80.     for(i = 0;i <length;i++)  
  81.         str[i] = '\0';  
  82.     len = length;  
  83. }  
  84.   
  85. String::String()  
  86. {  
  87.   
  88.     len = 0;  
  89.     str = new char[1];  
  90.   
  91.     str[0] = '\0';  
  92.   
  93. }  
  94.   
  95. String::~String()  
  96. {  
  97.   
  98.     delete []str;  
  99.     len = 0;  
  100. }  
  101.   
  102. String::String(const String &rs)  
  103. {  
  104.   
  105.     len = rs.getlen();  
  106.     str = new char[len + 1];  
  107.     for(int i =0 ;i < len ;i++ )  
  108.         str[i] = rs[i];  
  109.     str[len] = '\0';  
  110.   
  111.   
  112. }  
  113.   
  114. char &String ::operator [](unsigned short int length)  
  115. {  
  116.   
  117.     if(length > len)  
  118.         return str[len - 1];  
  119.     else  
  120.         return str[length];  
  121.   
  122.   
  123. }  
  124.   
  125. char String::operator [](unsigned short int length) const  
  126. {  
  127.   
  128.         if(length > len)  
  129.         return str[len - 1];  
  130.     else  
  131.         return str[length];  
  132.   
  133.       
  134.   
  135. }  
  136.   
  137. String &String::operator =(const String &s)  
  138. {  
  139.   
  140.     if(this == &s)  
  141.     return *this;  
  142.     delete []str;  
  143.     len = s.getlen();  
  144.     str = new char[len + 1];  
  145.     for(int i=0;i<len;i++)  
  146.     {  
  147.       
  148.         str[i] = s[i];  
  149.   
  150.     }  
  151.     str[len] = '\0';  
  152.   
  153.     return *this;  
  154.   
  155.   
  156.   
  157.   
  158. }  
  159.   
  160. String String::operator +(const String &rs)  
  161. {  
  162.   
  163.     int total = len + rs.getlen();  
  164.     String temp(total);  
  165.     int i,j;  
  166.     for( i = 0;i < len;i++)  
  167.         temp[i] = str[i];  
  168.     for(j = 0;j<rs.getlen();j++,i++)  
  169.         temp[i] = rs[j];  
  170.     temp[total] = '\0';  
  171.       
  172.     return temp;  
  173.   
  174.   
  175. }  
  176.   
  177. void String::operator +=(const String &rs)  
  178. {  
  179.   
  180.     int total = len + rs.getlen();  
  181.     String temp(total);  
  182.     int i,j;  
  183.     for(i = 0;i < len;i++)  
  184.         temp[i] = str[i];  
  185.     for(j = 0;j < rs.getlen();j++,i++)  
  186.         temp[i] = rs[j];  
  187.     temp[total] = '\0';  
  188.     *this = temp;  
  189.   
  190. }  
  191.   
  192. String::String(const charconst ch)  
  193. {  
  194.   
  195.     len = strlen(ch);  
  196.     str = new char[len+1];  
  197.     for(int i = 0;i < len;i++)  
  198.         str[i] = ch[i];  
  199.     str[len] = '\0';  
  200. }  
  201.   
  202.   
  203. int main()  
  204. {  
  205.   
  206.     String s1;  
  207.     cout<<"s1的长度:"<<s1.getlen()<<endl;  
  208.     char*temp = "help me";  
  209.     s1 = temp;  
  210.     cout<<"s1: "<<s1.getstr()<<"\t s1的长度: "<<s1.getlen()<<endl;  
  211.     char ch[10];  
  212.     strcpy(ch,"all right");  
  213.     s1 += ch;  
  214.     cout<<"ch:\t"<<ch<<endl;  
  215.     cout<<"s1: \t"<<s1.getstr()<<endl;  
  216.     cout<<"s1:"<<s1<<endl;  
  217.   
  218.     s1[2] = 'o';  
  219.     cout<<"s1:"<<s1<<endl;  
  220.     cout<<"s1[999]: "<<s1[999]<<endl;  
  221.     String s2 = "mother";  
  222.   
  223.     String s3("Mother");  
  224.     cout<<"s2: "<<s2<<"\t s3: "<<s3<<endl;  
  225.     String s4 = s2 + s3;  
  226.     cout<<" s2 + s3 = "<<s4<<endl;  
  227.     int check = s2>s3;  
  228.     cout<<"s2>s3: "<<check<<endl;  
  229.     check = s2 < s3;  
  230.     cout<<"s2<s3: "<<check<<endl;  
  231.   
  232.     check = s2 == s2;  
  233.     cout<<"s2 == s2: "<<check<<endl;  
  234.     cin>>s2[0]>>s3[0];  
  235.     s2 = s2 + s3;  
  236.     cout<<"s2: "<<" \t"<<s2<<endl;  
  237.   
  238.     return 0;  
  239. }  


运行的结果是:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值