字符串类型字符串类型
#include
#include
#include
class string{
unsigned len;//??ÖÐ×Ö?û?öÊý
unsigned maxlen;//×î?ó×Ö?û?öÊý
char *str;
public:
string(const char *a);
string( string &a);
string(){maxlen=10;len=0;str=new char [maxlen+1];}
~string(){delete []str;}
string op...
字符串类型
#include
#include
#include
class string{
unsigned len;//??ÖÐ×Ö?û?öÊý
unsigned maxlen;//×î?ó×Ö?û?öÊý
char *str;
public:
string(const char *a);
string( string &a);
string(){maxlen=10;len=0;str=new char [maxlen+1];}
~string(){delete []str;}
string operator+(const string &b);
string operator+(const char *b);
friend string operator+(const char *a,const string &b);
string operator=(const string &b);
string operator-(const string &b);
string operator-(const char *b);
friend string operator-(char *b,const string &a);
friend string operator-(char a,const string &b);
char * findsub(const string &b);
char * findsub(const char *b);
int seeksub(const string &b);
int seeksub(const char *b);
int replacesub(const string &b,const string &c);
int replacesub(const char *b,const char *c);
int replacesub(const string &b,const char *c);
int replacesub(const char *b,const string &c);
int size(){return len;}
friend ostream & operator<<(ostream &out,const string &a)
{
out<>(istream &in,string &a)
{ a.len=-1;
do
{
a.len++;
a.str[a.len]=in.get();
if(a.len==a.maxlen)
{ char *p=new char [a.len];
strncpy(p,a.str,a.len+1);
delete []a.str;
a.maxlen=2*a.len;
a.str=new char[a.maxlen+1];
strncpy(a.str,p,a.len+1);
}
}while(a.str[a.len]!=' '&&a.str[a.len]!='\n');
a.str[a.len]='\0';
return in;
}
};
string::string(const char *a) { len=strlen(a);
maxlen=2*len;
str=new char[maxlen+1];
strcpy(str,a);
}
string::string(string &a)
{
len=a.len;
maxlen=a.maxlen;
str=new char[maxlen+1];
strcpy(str,a.str);
}
string string::operator+(const char *b)
{
char *p=new char[len+strlen(b)+1];
strcpy(p,str);
strcat(p,b);
return string(p);
}
string string::operator +(const string &b)
{
return *this+b.str;
}
string operator+(const char *a,const string &b)
{
string aa(a);
return aa+b.str;
}
string string::operator-(const char *b)
{
unsigned len1=strlen(b);
unsigned sublen;
char *p,*sub,*q;
if(lenreplacesub(b.str,c.str); }
int string::replacesub(const string &b,const char *c)
{
return this->replacesub(b.str,c); }
int string::replacesub(const char *b,const string &c)
{
return this->replacesub(b,c.str); }
int string::seeksub(const char *b) {
char *p;
p=this->findsub(b);
if(p)
return (p-this->str);
else return 0;
}
int string::seeksub(const string &b) {
return this->seeksub(b.str); }
void main()
{
string a("I like C++ !");
char bb[]="I think you too,are you?";
string b(bb);
string c(a);
string d("like");
char *p;
cout<<"a="<>ef;
cout<<"ÄúÊäÈëµÄ×Ö?û??Ϊ:"<
本文档为【字符串类型】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。