字符串类设计及测试
#ifndef STRING_H
#define STRING_H
const int maxlen=128;
class String
{
private:
int curlen;
char* ch;
public:
//构造函数
String(char* init="");
String(const String& ob);
//析构函数
~String()
{
delete[] ch;
}
//赋值运算符
String& operator=(const String& ob);
String& operator=(char* s);
//关系运算符
int operator==(const String& ob) const
{
return strcmp(ch,ob.ch)==0;
}
int operator==(char* s) const
{
return strcmp(ch,s)==0;
}
friend int operator==(char* s,const String& ob)
{
return strcmp(s,ob.ch)==0;
}
int operator!=(const String& ob) const
{
return strcmp(ch,ob.ch)!=0;
}
int operator!=(char* s) const
{
return strcmp(ch,s)!=0;
}
friend int operator!=(char* s,const String& ob)
{
return strcmp(s,ob.ch)!=0;
}
int operator<(const String& ob) const
{
return strcmp(ch,ob.ch)<0;
}
int operator<(char* s) const
{
return strcmp(ch,s)<0;
}
friend int operator<(char* s,const String& ob)
{
return strcmp(s,ob.ch)<0;
}
int operator<=(const String& ob) const
{
return strcmp(ch,ob.ch)<=0;
}
int operator<=(char* s) const
{
return strcmp(ch,s)<=0;
}
friend int operator<=(char* s,const String& ob)
{
return strcmp(s,ob.ch)<=0;
}
int operator>(const String& ob) const
{
return strcmp(ch,ob.ch)>0;
}
int operator>(char* s) const
{
return strcmp(ch,s)>0;
}
friend int operator>(char* s,const String& ob)
{
return strcmp(s,ob.ch)>0;
}
int operator>=(const String& ob) const
{
return strcmp(ch,ob.ch)>=0;
}
int operator>=(char* s) const
{
return strcmp(ch,s)>=0;
}
friend int operator>=(char* s,const String& ob)
{
return strcmp(s,ob.ch)>=0;
}
//串拼接运算符
String operator+(const String& ob) const;
String operator+(char* s) const;
friend String operator+(char* s,const String& ob);
String& operator+=(const String& ob);
String& operator+=(char* s);
//有关串函数
//从start位置开始寻找字符c
int Find(char c,int start) const;
//找字符c最后出现的位置
int FindLast(char c) const;
//取子串
String Substr(int index,int count) const;
//String 的下标运算
char& operator[](int i);
friend ostream& operator<<(ostream& ostr,const String& s);
friend istream& operator>>(istream& istr,String& s);
//其他函数
int Length() const
{
return curlen-1;
}
int StrEmpty() const
{
return curlen==1;
}
void Clear();
};
#endif
--------------------next---------------------
阅读(1080) | 评论(0) | 转发(0) |