String 类的完整实现

来源:互联网 发布:js设置滚动条位置 编辑:程序博客网 时间:2024/05/17 05:18
class String
{
     char *name;
 public:
   
    String(); // String a,b;
    String(char *p);  // String a("abc");
String::String(const String &other)

    ~String();   //把申请的空间释放掉   
    String& operator+(const char* p);  //连接常量字符串
    String& operator+(const String &p); //连接对象字符串
    String& operator+=(const String &p); //连接对象字符串
    String& operator-(const String &p); //连接对象字符串
    String& operator-=(const String &p); //连接对象字符串
    String& operator=(const char* p);  //完成常量给对象赋值
    String& operator=(const String& p); //完成对象之间的赋值
    bool operator==(const char* p);  //完成对象值的判断      
   bool operator==(const String& p);
   bool operator>(const String& p);
   bool operator<(const String& p);
   char& String:: operator[]( int n)  //重载下标操作符


    friend istream& operator>>(istream& input,String& p) //输入流的重载
{
           input>>p.name;
           return input;
}
    friend ostream& operator<<(ostream& output,const String& p) //输出流的重载
{
             output<<p.name;
             return output;
}   
             
  };
     
//实现部分     
 //默认构造函数    
String::String() // String a,b;
{m_data = new char[1];
       *m_data  = '\0';
}
  
String::String(char *p)  // String a("abc");
 if(NULL ==p )
     {
           name = new char[1];
           *name = '\0';
      }
 else
{
name=new char[strlen(p)+1];
if(name!=0) strcpy(name,p);           
}


String::~String()   //把申请的空间释放掉
{
delete[] name;       
}  
//拷贝构造函数
String::String(const String &other)         //用一个string对象来初始化要定义的string对象
{
      int iLen = strlen(other.name);
      name = new char[iLen + 1];
     strcpy(name, other.name);    
}           
   
String&  String::operator+(const char *p)  //连接常量字符串
{
   strcat(name,p);      
   
   return *this;        
}
        
String&  String::operator+=(const String &p) //连接对象字符串
{
  char* temp;
   
      temp=new char[strlen(name)+1+strlen(p.name)+1];
      strcpy(temp,name);
      strcat(temp,p.name);
      name=temp;
   return *this;   
   /*或者调用operator+
  name=name+p.name;
   return *this;*/
}
String  String::operator+(const String &p) //连接对象字符串
{
  char* temp;
      int newlen=strlen(name)+strlen(p.name);
      temp=new char[newlen+2];
      strcpy(temp,name);
      strcat(temp,p.name);
 temp[newlen]='\0';
String str(temp);
   return str;        
}
//重载 "-=": 
String& String::operator-=(const String &other) 
{  char *temp,*p; 
 if( (temp=strstr(name,other.name)) ==NULL) 
 {    cout<<"没有符合的子串,不能使用'-'操作"<<endl; 
    return *this; 
 }  
 else  
 {      p=temp;  
       temp=temp+strlen(other.name); 
  *p='\0'; 
  strcat(name,temp); 
 } 
 cout<<"重载了运算符-"<<endl; 
 return *this;
 /*或者调用--,name=name-p.name;
               return *this;*/
 }  
//重载 "-": 
String String::operator-(const String &other) 
{  char *temp,*p,*temp2=new [strlen(name)+1]; 
                  strcpy(temp2,name);
 if( (temp=strstr(temp2,other.name)) ==NULL) 
 {    cout<<"没有符合的子串,不能使用'-'操作"<<endl; 
    return temp2; 
 }  
 else  
 {      p=temp;  
       temp=temp+strlen(other.name); 
  *p='\0'; 
  strcat(temp2,temp); 
 } 
 cout<<"重载了运算符-"<<endl; 
 return temp2;

 }  
   
  
String&  String::operator=(const char *p)  //完成常量给对象赋值
{
  name = new char[strlen(p)+1];
  if(name!=0) strcpy(name,p);
  return *this;
  }  
             
String&  String::operator=(const String& p) //完成对象之间的赋值
{
 if(this!=&p) { 
  delete name;
  name = new char[strlen(p.name)+1];
  if(name!=0) strcpy(name,p.name);
 
  }
 return *this;
}  


bool String::operator==(const char* p)  //完成对象值的判断 
{
 bool isEqual=true;
 
    while(*name!='\0')
    {
      if(*name!=*p)
 {
       isEqual=false;
  break;
 }
       
       name++;
       p++;                   
     }
    return isEqual;    
 } 
bool String::operator==(const String& p)  //完成对象值的判断 

if(strcmp(name,p.name)==0)
return true;

return false;
 } 
bool String::operator>(const String& p)  //完成对象值的判断 

if(strcmp(name,p.name)>0)
return true;

return false;
 } 
bool String::operator<(const String& p)  //完成对象值的判断 

if(strcmp(name,p.name)<0)
return true;

return false;
 } 
char& String:: operator[]( int n)  //重载下标操作符

  {

      if(n>strlen(name))


          return name[length-1];

      else

          return name[n];

  }

在String类中增加长度后的实现如下:

原文:http://www.oschina.net/code/snippet_567425_11395

class _string
02{
03    friend std::istream& operator>>(std::istream& is, _string& a);
04    friend std::ostream& operator<<(std::ostream& os,_string& a);
05     
06public:
07    _string()     //默认构造函数
08    {
09        length = 0;
10        b=new char[1];
11        b[0]='\0';
12   }            
13    _string(char *a);   //构造函数
14    _string(int n,char a);
15    ~_string(); //析构函数
16    _string(_string &a);  //复制构造函数
17    int size(){return length;}  //获得字符串长度
18    _string operator+(const _string& a);   //重载'+'操作符
19    _string& operator+=(const _string& a);   //重载'+='操作符
20    _string& operator=(const _string& a);   //重载赋值操作符
21    char& operator[]( int n);   //重载下标操作符
22    _string  substr(int pos,int n);   //返回子字符串
23   _string  substr(int pos);   //返回子字符串
24 
25 
26 private:
27    char *b;
28    int length;
29};
// _string.cpp : 定义控制台应用程序的入口点。
002//
003 
004#include "stdafx.h"
005#include<iostream>
006#include<string.h>
007#include"_string.h"
008#include<stdlib.h>
009using namespace std;
010 
011_string::_string(char *a)     //构造函数
012{
013    length = strlen(a);  
014    b = new char[length+1];
015    for(int  i= 0;i<length;i++)
016    {
017        b[i] = a[i];
018    }
019    b[length] = '\0';
020}
021 
022_string::_string(int n,char a)
023{
024    b=new char[n+1];
025    for(int  i= 0;i<n;i++)
026    {
027        b[i] =a;
028    }
029    b[n] = '\0';
030}
031 
032_string::~_string()     //析构函数
033{
034    delete []b;
035    length=0;
036}
037 
038_string::_string(_string &a)      //复制构造函数
039{
040    length=a.size();
041    b=new char [length+1];
042   for(int  i = 0;i<length;i++)
043    {
044        b[i] = a.b[i];
045    }
046    b[length] = '\0';
047}
048 
049_string  _string::operator+(const _string& a)    //重载+
050{
051    int newLen = length+a.length;
052    char *str;
053    str = new char[newLen+1];
054    int count = 0;
055    for(int i = 0;i<length;i++)
056    {
057        str[i] = this->b[i];
058        count++;
059    }
060     
061    for(int i =0;count<newLen;count++,i++)
062    {
063        str[i] = a.b[i];
064    }
065    str[newLen] = '\0';
066    _string temp(str);
067     
068    return temp;
069}
070 
071_string&  _string:: operator+=(const _string& a)   //重载+=
072{
073    int newLen = length+a.length;
074    char *str;
075    str = new char[newLen+1];
076    int count = 0;
077    for(int i = 0;i<length;i++)
078    {
079        str[i] = this->b[i];
080        count++;
081    }
082     
083    for(int i =0;count<newLen;count++,i++)
084    {
085        str[i] = a.b[i];
086    }
087    str[newLen] = '\0';
088    _string temp(str);
089    *this=temp;
090    return *this;
091    }
092_string& _string:: operator=(const _string &a)    //重载=
093  {
094      if(this==&a)
095          return *this;
096 
097      delete []b;
098      length = a.length;
099      b = new char[length+1];
100      for(int  i = 0;i<length;i++)
101    {
102        b[i] = a.b[i];
103    }
104    b[length] = '\0';
105          return *this;
106 
107}
108 
109  char& _string:: operator[]( int n)  //重载下标操作符
110  {
111      if(n>length)
112          return b[length-1];
113      else
114          return b[n];
115  }
116 
117  ostream& operator<<(ostream& os, _string& a)  //重载输出符
118  {
119      os<<a.b;
120      return os;
121  }
122  istream& operator>>(std::istream& is, _string& a)  //重载输入符
123  {  
124      is>>a.b ;
125      a.length=strlen(a.b);
126      return is;
127  }
128 
129 _string  _string::substr(int pos, int n)    //两个接受不同参数的substr函数,返回子字符串
130  {
131      char *p = new char[n+1];
132      for(int i=0;i<n;i++)
133      {
134          p[i]=b[pos];
135          pos++;
136      }
137      p[n]='\0';
138      _string k(p);
139      k.length=n;
140      return  k;
141 }
142      
143 _string  _string::substr(int pos)
144 {
145     int len=length;
146     char *p=new char[len-pos+1];
147     int t=pos;
148     for(int i=0;t<len;t++,i++)
149     {
150         p[i]=b[t];
151     }
152     p[t]='\0';
153      _string k(p);
154      k.length=len-pos;
155      return  k;
156 }
#include "stdafx.h"
02#include<iostream>
03#include"_string.h"
04 
05using namespace std;
06int main()
07{
08    /*_string a("1234");
09    cout<<a.size()<<endl;
10    _string b("5678");
11    a+=b;
12    cout<<a.size()<<endl;
13    _string aa("111");
14    _string bb("222");
15    _string c;
16    c=aa+bb;
17    cout<<"c size  1  "<<c.size()<<endl;
18    c=aa;
19    cout<<"c size  2  "<<c.size()<<endl;
20   c="1234567";
21    cout<<"c size  3  "<<c.size()<<endl;
22    cout<<c<<endl;
23    _string xxx(10,'2');
24    cout<<"xxx is  "<<xxx<<endl;
25    _string str;
26    cout<<"请输入str"<<endl;
27    cin>>str;
28    cout<<"你输入的是:";
29    cout<<str<<endl;
30    cout<<"你输入的字符串长度为: "<<str.size()<<endl;
31    cout<<"substr"<<endl;
32    _string abc("1234567");
33    _string xx=abc.substr(3,3);
34    cout<<"xx is "<<xx<<endl;
35    _string qq=abc.substr(3);
36    cout<<"qq is "<<qq<<endl;*/
37_string a;
38cin>>a;
39cout<<a;
40return 0;
41}

陈皓博客中的string类实现:

http://coolshell.cn/articles/10478.html


0 0