定义一个基本的 String 类
class String
{
public :
String():base(" " ){}
private :
char * base;
long int length;
} ;
我们可以看到在这个类里面我们用一个char 型指针来保存字符串值,long int 型值length 来保存字符串的长度。
① 由于我们知道string 类 支持这样的构造函数:
string str( “hello”);
string str(5,’c’);
string str1(str,2);
string str2(str,0,2);
故我们给我们的String 类 添加相应的构造函数:
class String
{
public :
String():base("" ),length(0){}
String(const char * p)
{
if (p == NULL)
{
base = new char [1];
base = '/0' ;
}
else
{
base = new char [strlen(p)+ 1];
strcpy(base,p);
}
length = strlen(p);
}
String(int count,char ch)
{
base = new char [count+ 1];
for (int i= 0; i< count; i++ )
{
base[i] = ch;
}
base[++ i] = '/0' ;
length = count;
}
String(String& str,int count)
{
base = new char [count+ 1];
strncpy(base,str. base,count);
base[count] = '/0' ;
length = count;
}
String(String& str,int index,int count)
{
base = new char [count+ 1];
for (int i= index,j= 0; i< count+ index; i++ )
{
base[j++ ] = str. base[i];
}
base[j] = '/0' ;
length = count;
}
private :
char * base;
long int length;
} ;
② 为类添加拷贝构造函数和析构函数
拷贝构造函数:
String(const String & str)
{
base = new char [str. length+ 1];
strcpy(base,str. base);
}
注意这里的拷贝构造函数的形参是定义为const ,为什么要定义为const 呢?是因为要既能 接收const 实参,又能接受非const 实 参。若是我们定义为非const ,则无法接受const 实 参了。那为什么我们不定义函数重载呢。那是一个类只能有一个复制构造函数啦。
析构函数:
~ String()
{
if (length != 0)// 防止空串情况。
delete base;
}
① string 类里面的操作符重载
输出操作符 (“<<”):
friend ostream& operator << (ostream& os,const String & str)
{
return os << str. base;
}
定义为友元函数。
输入操作符 (“>>”) :
#define STR_SIZE 1024
friend istream& operator >> (istream& is,String & str)
{
str. base = new char [STR_SIZE];
return is >> str. base;
}
加 (“+=”) 操作符 :
String& operator += (const String& str)
{
char * s = new char [length+ 1];
strcpy(s,base);
base = new char [length+ str. length+ 1];
strcpy(base,s);
strcat(base,str. base);
length += str. length;
return * this ;
}
String& operator += (const char * p)
{
char * s = new char [length+ 1];
strcpy(s,base);
base = new char [length+ strlen(p)+ 1];
strcpy(base,s);
strcat(base,p);
length += strlen(p);
return * this ;
}
String& operator += (const char p)
{
char * s = new char [length+ 1];
strcpy(s,base);
base = new char [length+ 2];
strcpy(base,s);
base[length] = p;
base[length+ 1] = '/0' ;
length += 1;
return * this ;
}
加 (“+”) 操作符
friend String operator + (String& str1,const String& str2)
{
String ret(str1);
ret += str2;
return ret;
}
friend String operator + (String& str1,const char * p)
{
String ret(str1);
ret += p;
return ret;
}
friend String operator + (String& str1,const char p)
{
String ret(str1);
ret += p;
return ret;
}
赋值 (“=”) 操作符
String& operator = (const String& str2)
{
base = new char [str2. length+ 1];
strcpy(base,str2. base);
length = str2. length;
return * this ;
}
String& operator = (const char * p)
{
base = new char [strlen(p)+ 1];
strcpy(base,p);
length = strlen(p);
return * this ;
}
String& operator = (const char p)
{
base = new char [2];
base[0] = p;
base[1] = '/0' ;
return * this ;
}
关系操作符重载:
相等 (“==”) 操作符 :
friend bool operator == (const String& str1,const String& str2)
{
int result = strcmp(str1. base,str2. base);
if (result != 0)
{
return false ;
}
else
{
return true ;
}
}
friend bool operator == (const String& str1,const char * p)
{
int result = strcmp(str1. base,p);
if (result != 0)
{
return false ;
}
else
{
return true ;
}
}
不等 (“!=”) 操作符 :
friend bool operator != (const String& str1,const String& str2)
{
return ! (str1 == str2);
}
friend bool operator != (const String& str1,const char * p)
{
return ! (str1 == p);
}
小于 (“<”):
friend bool operator < (const String& str1,const String& str2)
{
int result = strcmp(str1. base,str2. base);
if (result < 0)
{
return true ;
}
else
{
return false ;
}
}
friend bool operator < (const String& str1,const char * p)
{
int result = strcmp(str1. base,p);
if (result < 0)
{
return true ;
}
else
{
return false ;
}
}
大于 (“>”) 操作符:
friend bool operator > (const String& str1,const String& str2)
{
int result = strcmp(str1. base,str2. base);
if (result > 0)
{
return true ;
}
else
{
return false ;
}
}
friend bool operator > (const String& str1,const char * p)
{
int result = strcmp(str1. base,p);
if (result > 0)
{
return true ;
}
else
{
return false ;
}
}
小于等于 (“<=”) :
friend bool operator <= (const String& str1,const String& str2)
{
int result = strcmp(str1. base,str2. base);
if (result <= 0)
{
return true ;
}
else
{
return false ;
}
}
friend bool operator <= (const String& str1,const char * p)
{
int result = strcmp(str1. base,p);
if (result <= 0)
{
return true ;
}
else
{
return false ;
}
}
大于等于 (“>=”):
friend bool operator >= (const String& str1,const String& str2)
{
int result = strcmp(str1. base,str2. base);
if (result >= 0)
{
return true ;
}
else
{
return false ;
}
}
friend bool operator >= (const String& str1,const char * p)
{
int result = strcmp(str1. base,p);
if (result >= 0)
{
return true ;
}
else
{
return false ;
}
}
下标 (“[]”) 操作符 :
char operator [](int index)
{
return base[index];
}
String 类的常用成员函数:
得到长度 :
long int Length()
{
return length;
}
判断是否为空串 :
bool empty()
{
if (length == 0)
{
return true ;
}
else
{
return false ;
}
}
判断大小函数 :
long int size()
{
return length* sizeof (char );
}
字串操作 :
String substr(long int pos,long int n)
{
String ret;
ret. base = new char [n+ 1];
for (int i= pos,j= 0; i < pos+ n; ++ i,++ j)
{
ret. base[j] = base[i];
}
ret. base[j] = '/0' ;
ret. length = n;
return ret;
}
String substr(long int pos)
{
String ret;
ret. length = length- pos+ 1;
ret. base = new char [ret. length];
for (int i= pos,j= 0; i< length; ++ i,++ j)
{
ret. base[j] = base[i];
}
ret. base[j] = '/0' ;
return ret;
}
String substr()
{
String ret;
ret. base = new char [length+ 1];
strcpy(ret. base,base);
ret. length = length;
return ret;
}