📄 string.cpp
字号:
#include "String.h"
String::String(const String & ob)
{
curlen=ob.curlen;
ch=new char[curlen];
assert(ch!=NULL);
strcpy(ch,ob.ch);
}
String::String(char *init)
{
curlen=strlen(init)+1;
ch=new char[curlen];
assert(ch!=NULL);
strcpy(ch,init);
}
String & String::operator =(const String & ob)
{
if(curlen!=ob.curlen)
{
delete []ch;
ch=new char[ob.curlen];
if(ch==NULL)
{
cout<<"Invalid Error"<<endl;
exit(1);
}
curlen=ob.curlen;
}
strcpy(ch,ob.ch);
return *this;
}
String & String::operator =(char *s)
{
int len=strlen(s)+1;
if(len!=curlen)
{
delete []ch;
ch=new char[strlen(s)+1];
if(ch==NULL)
{
cout<<"Invalid Error"<<endl;
exit(1);
}
curlen=strlen(s)+1;
}
strcpy(ch,s);
return *this;
}
ostream & operator <<(ostream & os,const String & s)
{
os<<s.ch;
return os;
}
istream & operator >>(istream & is,String & s)
{
s.ch=new char[maxlen];
if(s.ch==NULL)
{
cerr<<"Invalid Error"<<endl;
exit(1);
}
is>>s.ch;
return is;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -