*/ --------------------------------------------------------------------------------------
*/ 出自: 编程中国 http://www.bc-cn.net
*/ 作者: 中学者
*/ 时间: 2007-11-18 编程论坛首发
*/ 声明: 尊重作者劳动,转载请保留本段文字
*/ --------------------------------------------------------------------------------------
最近学了操作符重载,自己写了一个很小很小很小的部分功能的string类,希望大家给点意见~
/***------------------string.h---------------------------------------****/
class string{
public:
string(); //创建一个空字符串
string(const char*); //创建一个字符串
string(const string&); //拷贝字符串
~string();
const string& operator= (const string&); //赋值给字符串
const string& operator= (const char*);
const string& operator= (char);
string& operator+ (const string&); //连接字符串
string& operator+ (const char*);
string& operator+ (char);
string& operator+= (const string&); //连接字符串,返回更新后的对象
string& operator+= (const char*);
string& operator+= (char);
friend ostream& operator<<(ostream&,const string&); //输出字符串
friend istream& operator>>(istream&,string&); //输入字符串
bool operator== (const string&); //判断字符串相等
bool operator!= (const string&); //判断字符串不相等
int length() const; //返回对象长度
int Insert(char*,int=0/*字符串长度*/,int=0/*索引*/); //添加字串到指定位置
private:
char* str;
int count;
enum{Maxsize=100};
private:
void CopyObject(const string&); //拷贝函数
};
/****--------------------------------------------------------------------------
-------------------------string.cpp---------------------------------------------
-----------------------------------------------------------------------------*/
/**------------创建空串:string::string();----------------***/
string::string():count(-1)
{
str=new char[Maxsize];
}
/*--------------创建串:string::string(const char*);------------**/
string::string(const char* str):count(strlen(str)-1)
{
this->str=new char[Maxsize];
for(int i=0;i<=count;i++)
this->str[i]=str[i];
}
/*----------拷贝构造函数:string::string(const string&);----------------**/
string::string(const string&obj):str(0)
{
CopyObject(obj);
}
void string::CopyObject(const string&obj)
{
delete [] str;
if(obj.str!=NULL)
{
str=new char[Maxsize];
count=obj.count;
for(int i=0;i<=count;i++)
str[i]=obj.str[i];
}
else
{
str=NULL;
count=-1;
}
}
/**----------------析构函数:string::~string();------------------**/
string::~string()
{
delete [] str;
count=-1;
}
/**--------------赋值操作:string& string::operator=(const string&);-------
--------------------------string& string::operator=(const char*);---------
--------------------------string& string::operator=(char);-------------***/
const string& string::operator= (const string& obj)
{
if(this!=&obj)
CopyObject(obj);
return *this;
}
const string& string::operator= (char c)
{
if(count!=-1)
{
delete [] str;
str=new char[Maxsize];
count=-1;
str[++count]=c;
}
else
{
str[++count]=c;
}
return *this;
}
const string& string::operator= (const char* str)
{
if(count==-1)
{
for(int i=0;i<static_cast<int>(strlen(str));i++)
{
this->str[++count]=str[i];
}
}
else
{
delete [] this->str;
this->str=new char[Maxsize];
count=-1;
for(int i=0;i<static_cast<int>(strlen(str));i++)
{
this->str[++count]=str[i];
}
}
return *this;
}
/***---------输入函数:istream& operator>>(istream&,string&);-----------***/
istream& operator>>(istream& in,string& obj)
{
in>>obj.str;
obj.count=strlen(obj.str)-1;
return in;
}
/**----------输出函数:ostream& operator<<(ostream&,const string&);---------***/
ostream& operator<<(ostream& out,const string&obj)
{
for(int i=0;i<=obj.count;i++)
out<<obj.str[i];
return out;
}
/***-------连接字符串: string& string::operator+( const string&);-------
-----------------------string& string::operator+(const char*);----------
-----------------------string& string::operator+(char);--------------**/
string& string::operator+ (const string& obj)
{
static string oobj;
for(int i=0;i<=count;i++)
oobj.str[++oobj.count]=str[i];
for(int j=0;j<=obj.count;j++)
oobj.str[++oobj.count]=obj.str[j];
return oobj;
}
string& string::operator+ (const char* str)
{
static string obj;
for(int i=0;i<=count;i++)
obj.str[++obj.count]=this->str[i];
for(int j=0;j<static_cast<int>(strlen(str));j++)
obj.str[++obj.count]=str[j];
return obj;
}
string& string::operator+ (char c)
{
static string obj;
for(int i=0;i<=count;i++)
obj.str[++obj.count]=this->str[i];
obj.str[++obj.count]=c;
return obj;
}
/***---------连接字符串:string& string::operator+=( const string&);-------
------------------------string& string::operator+=( const char*);---------
------------------------string& string::operator+=(char)---------------***/
string& string::operator+= (const string& obj)
{
for(int i=0;i<=obj.count;i++)
str[++count]=obj.str[i];
return *this;
}
string& string::operator+= (const char* str)
{
for(int i=0;i<strlen(str);i++)
this->str[++count]=str[i];
return *this;
}
string& string::operator+= (char c)
{
str[++count]=c;
return *this;
}
/***-----判断字符串是否相等:bool string::operator==(const string&);----------
***-------------------------bool string::operator!=(const string&);--------**/
bool string::operator== (const string & obj)
{
int flag=-1;
if(count==obj.count)
{
for(int i=0;i<=count;i++)
if(str[++flag]==obj.str[i])
continue;
else
break;
}
else
return false;
if(flag!=count)
return false;
return true;
}
bool string::operator!= (const string& obj)
{
if(*this==obj)
return false;
else
return true;
}
/***----------返回对象长度:int string::length();-------***/
int string::length() const
{
return (count+1);
}
/**-----------添加字串到指定位置:int string::Insert(int=0,char*,int);------*/
int string::Insert(char *str,int l,int i)
{
if(i==0)
{
for(int j=count;j>=0;j--)
this->str[j+l]=this->str[j];
for(int n=0;n<l;n++)
this->str[n]=str[n];
count=length()+l-1;
}
else
{
int flag=-1;
if(i==length())
{
for(int j=i;j<i+l;j++)
this->str[j]=str[++flag];
flag=-1;
count=length()+l-1;
}
else
{
for(int j=count;j>=i;j--)
this->str[j+l]=this->str[j];
for(int n=i;n<i+l;n++)
this->str[n]=str[++flag];
flag=-1;
count=length()+l-1;
}
}
return i;
}