谁知道C++中的string类的定义和实现文件是怎么写的?
3个回答
展开全部
我以前自己写的,相对功能比较完全,
class substring;
class string
{
public:
string();
string(char);
string(unsigned);
string(const char *);
string(const string &);
~string();
friend class substring;
void operator = (const string & right);
void operator += (const string &v);
friend string operator+(const string &left,const string &right);
friend string operator*(const string &left,const int right);
friend void operator-(const string &left,const string &right);
/////////////////////////////////////////////////////////////////////
friend int operator>(const string &left,const string &right);
friend int operator>=(const string &left,const string &right);
friend int operator==(const string &left,const string &right);
friend int operator<(const string &left,const string &right);
friend int operator<=(const string &left,const string &right);
friend int operator!=(const string &left,const string &right);
//////////////////////////////////////////////////////////////////
void replace(string &base,string pattern,string rp);
int compare(const string & val)const;
friend int change(const string &str);
substring operator()(unsigned start,unsigned len);
istream & getline(istream &);
friend istream &operator>>(istream& cin,string& s);
unsigned length()const;
char & operator[](unsigned index)const;
operator const char *()const
{
return buffer;
}
private:
unsigned buflen;
char *buffer;
};
//////////////////////////////////////////////////////
string::string(unsigned size)
{
assert(size>=0);
buflen=1+size;
buffer=new char [buflen];
assert(buffer!=0);
for(unsigned i=0;i<buflen;i++)
buffer[i]='\0';
}
string::string(const char *inittext)
{
buflen=1+cstrLen(inittext);
buffer=new char[buflen];
assert(buffer!=0);
for(unsigned i=0;inittext[i]!='\0';i++)
buffer[i]=inittext[i];
buffer[i]='\0';
}
string::string(const string &initstr)
{
buflen=1+cstrLen(initstr.buffer);
buffer=new char[buflen];
assert(buffer!=0);
for(unsigned i=0;initstr.buffer[i]!='\0';i++)
buffer[i]=initstr.buffer[i];
buffer[i]='\0';
}
string::string(char c)
{
buflen=2;
buffer=new char[buflen];
assert(buffer!=0);
buffer[0]=c;
buffer[1]='\0';
}
string::string()
{
buflen=1;
buffer=new char[buflen];
assert(buffer!=0);
buffer[0]='\0';
}
string::~string()
{
delete[]buffer;
buffer=0;
}
/*void getCommand()
{
int i;
string prompt="command>";
double x;
}*/
unsigned string::length()const
{
return cstrLen(buffer);
}
int string::compare(const string & val)const
{
char *p=buffer;
char *q=val.buffer;
for(;(*p!='\0')&&(*p==*q);p++,q++)
;
return *p-*q;
}///////////////////////////////////////////////对运算的定义
void string::operator = (const string & right)
{
const unsigned rightLength=right.length();
if(right.length()>=buflen)
{
delete[]buffer;
buflen=1+rightLength;
buffer=new char[buflen];
assert(buffer!=0);
}
for(unsigned i=0;right.buffer[i]!='\0';i++)
buffer[i]=right.buffer[i];
buffer[i]='\0';
}
void string::operator += (const string &v)
{
unsigned i;
unsigned conLen=length()+v.length();
if(conLen>=buflen)
{
char *newbuf=new char[1+conLen];
assert(newbuf!=0);
for(i=0;buffer[i]!='\0';i++)
newbuf[i]=buffer[i];
delete []buffer;
buflen=1+conLen;
buffer=newbuf;
}
else
i=cstrLen(buffer);
for(unsigned j=0;v.buffer[j]!='\0';i++,j++)
buffer[i]=v.buffer[j];
buffer[i]='\0';
}
string operator+(const string &left,const string &right)
{
string result(left);
result+=right;
return result;
}
string operator*(const string &left,const int right)
{
string result(left);
for(int j=0;j<right;j++)
result+=left;
return result;
}
void operator-(const string &left,const string &right)
{
unsigned i;
bool flag=false;
for(unsigned j=0;j<left.buflen;j++)
{
for(i=0;(right[i]!=left[j])||(right[i]='\0');j++,i++)
{
if(i=right.buflen-1)flag=true;
}
}
if(flag)
{
for(unsigned k=i-right.buflen+unsigned(2);k<j;k++)
{
left[k]='\0';
}
}
}
///////////////////////////////////////////////////
char & string::operator[](unsigned index)const
{
if(index>=cstrLen(buffer))
{
nothing='\0';
return nothing;
}
return buffer[index];
}
////////////////////////////////////////////////////////
int operator!=(const string &left,const string &right)
{
return left.compare(right)!=0;
}
int operator==(const string &left,const string &right)
{
return left.compare(right)==0;
}
int operator>(const string &left,const string &right)
{
return left.compare(right)!=0;
}
int operator>=(const string &left,const string &right)
{
return left.compare(right)!=0;
}
int operator<(const string &left,const string &right)
{
return left.compare(right)!=0;
}
int operator<=(const string &left,const string &right)
{
return left.compare(right)!=0;
}
//////////////////////////////////////////////////
istream &operator>>(istream& cin,string& s)
{
for(unsigned i=0;i<s.buflen;i++)
cin>>s.buffer[i];
return cin;
}
//////////////////////////////////////////////////
int isLowerCase(char c)
{
return (c>='a')&&(c<='z');
}
///////////////////////
int isDigit(char c)
{
return (c>='0')&&(c<='9');
}
//////////////////////////////
int isAlphabetic(char c)
{
return((c>='a')&&(c<='z'))||((c>='A')&&(c<='Z'));
}
//////////////////////////
int isAlphnumber(char c)
{
return((c>='a')&&(c<='z'))||((c>='A')&&(c<='Z'))||((c>='0')&&(c<='9'));
}
//////////////////////////
void toUpper(string &word)
{
for(unsigned i=0;word[i]!='\0';i++)
if(isLowerCase(word[i]))
word[i]=(word[i]-'a')+'A';
}
int change(const string &str)
{
int a[10];
int r=0;
for(unsigned j=0,i=0;str[i]!='\0';i++)
{
if((str.buffer[i]>'0')&&(str.buffer[i]<'9'))
{
a[j]=str.buffer[i];
j++;
}
}
if(j==0)return 0;
else for(unsigned i=0;i<j;i++)
{
a[i]=a[i]-48;
int k=j;
for(;j>1+i;j--)
{
a[i]=a[i]*10;
}
j=k;
}
for(i=0;i<j;i++)
r+=a[i];
return r;
}
/////////////////////////////////////////////////////////////////////////
void string::replace(string &base,string pattern,string rp)
{
unsigned len=base.buflen,f=0;
base.buflen=(base.buflen+rp.buflen-pattern.buflen);
base=new char[base.buflen];
for(unsigned i=0;i<base.buflen;i++)
{
for(unsigned j=0;(base[i]=pattern[j])&&(j<pattern.buflen);i++,j++)
;
}
}
class substring;
class string
{
public:
string();
string(char);
string(unsigned);
string(const char *);
string(const string &);
~string();
friend class substring;
void operator = (const string & right);
void operator += (const string &v);
friend string operator+(const string &left,const string &right);
friend string operator*(const string &left,const int right);
friend void operator-(const string &left,const string &right);
/////////////////////////////////////////////////////////////////////
friend int operator>(const string &left,const string &right);
friend int operator>=(const string &left,const string &right);
friend int operator==(const string &left,const string &right);
friend int operator<(const string &left,const string &right);
friend int operator<=(const string &left,const string &right);
friend int operator!=(const string &left,const string &right);
//////////////////////////////////////////////////////////////////
void replace(string &base,string pattern,string rp);
int compare(const string & val)const;
friend int change(const string &str);
substring operator()(unsigned start,unsigned len);
istream & getline(istream &);
friend istream &operator>>(istream& cin,string& s);
unsigned length()const;
char & operator[](unsigned index)const;
operator const char *()const
{
return buffer;
}
private:
unsigned buflen;
char *buffer;
};
//////////////////////////////////////////////////////
string::string(unsigned size)
{
assert(size>=0);
buflen=1+size;
buffer=new char [buflen];
assert(buffer!=0);
for(unsigned i=0;i<buflen;i++)
buffer[i]='\0';
}
string::string(const char *inittext)
{
buflen=1+cstrLen(inittext);
buffer=new char[buflen];
assert(buffer!=0);
for(unsigned i=0;inittext[i]!='\0';i++)
buffer[i]=inittext[i];
buffer[i]='\0';
}
string::string(const string &initstr)
{
buflen=1+cstrLen(initstr.buffer);
buffer=new char[buflen];
assert(buffer!=0);
for(unsigned i=0;initstr.buffer[i]!='\0';i++)
buffer[i]=initstr.buffer[i];
buffer[i]='\0';
}
string::string(char c)
{
buflen=2;
buffer=new char[buflen];
assert(buffer!=0);
buffer[0]=c;
buffer[1]='\0';
}
string::string()
{
buflen=1;
buffer=new char[buflen];
assert(buffer!=0);
buffer[0]='\0';
}
string::~string()
{
delete[]buffer;
buffer=0;
}
/*void getCommand()
{
int i;
string prompt="command>";
double x;
}*/
unsigned string::length()const
{
return cstrLen(buffer);
}
int string::compare(const string & val)const
{
char *p=buffer;
char *q=val.buffer;
for(;(*p!='\0')&&(*p==*q);p++,q++)
;
return *p-*q;
}///////////////////////////////////////////////对运算的定义
void string::operator = (const string & right)
{
const unsigned rightLength=right.length();
if(right.length()>=buflen)
{
delete[]buffer;
buflen=1+rightLength;
buffer=new char[buflen];
assert(buffer!=0);
}
for(unsigned i=0;right.buffer[i]!='\0';i++)
buffer[i]=right.buffer[i];
buffer[i]='\0';
}
void string::operator += (const string &v)
{
unsigned i;
unsigned conLen=length()+v.length();
if(conLen>=buflen)
{
char *newbuf=new char[1+conLen];
assert(newbuf!=0);
for(i=0;buffer[i]!='\0';i++)
newbuf[i]=buffer[i];
delete []buffer;
buflen=1+conLen;
buffer=newbuf;
}
else
i=cstrLen(buffer);
for(unsigned j=0;v.buffer[j]!='\0';i++,j++)
buffer[i]=v.buffer[j];
buffer[i]='\0';
}
string operator+(const string &left,const string &right)
{
string result(left);
result+=right;
return result;
}
string operator*(const string &left,const int right)
{
string result(left);
for(int j=0;j<right;j++)
result+=left;
return result;
}
void operator-(const string &left,const string &right)
{
unsigned i;
bool flag=false;
for(unsigned j=0;j<left.buflen;j++)
{
for(i=0;(right[i]!=left[j])||(right[i]='\0');j++,i++)
{
if(i=right.buflen-1)flag=true;
}
}
if(flag)
{
for(unsigned k=i-right.buflen+unsigned(2);k<j;k++)
{
left[k]='\0';
}
}
}
///////////////////////////////////////////////////
char & string::operator[](unsigned index)const
{
if(index>=cstrLen(buffer))
{
nothing='\0';
return nothing;
}
return buffer[index];
}
////////////////////////////////////////////////////////
int operator!=(const string &left,const string &right)
{
return left.compare(right)!=0;
}
int operator==(const string &left,const string &right)
{
return left.compare(right)==0;
}
int operator>(const string &left,const string &right)
{
return left.compare(right)!=0;
}
int operator>=(const string &left,const string &right)
{
return left.compare(right)!=0;
}
int operator<(const string &left,const string &right)
{
return left.compare(right)!=0;
}
int operator<=(const string &left,const string &right)
{
return left.compare(right)!=0;
}
//////////////////////////////////////////////////
istream &operator>>(istream& cin,string& s)
{
for(unsigned i=0;i<s.buflen;i++)
cin>>s.buffer[i];
return cin;
}
//////////////////////////////////////////////////
int isLowerCase(char c)
{
return (c>='a')&&(c<='z');
}
///////////////////////
int isDigit(char c)
{
return (c>='0')&&(c<='9');
}
//////////////////////////////
int isAlphabetic(char c)
{
return((c>='a')&&(c<='z'))||((c>='A')&&(c<='Z'));
}
//////////////////////////
int isAlphnumber(char c)
{
return((c>='a')&&(c<='z'))||((c>='A')&&(c<='Z'))||((c>='0')&&(c<='9'));
}
//////////////////////////
void toUpper(string &word)
{
for(unsigned i=0;word[i]!='\0';i++)
if(isLowerCase(word[i]))
word[i]=(word[i]-'a')+'A';
}
int change(const string &str)
{
int a[10];
int r=0;
for(unsigned j=0,i=0;str[i]!='\0';i++)
{
if((str.buffer[i]>'0')&&(str.buffer[i]<'9'))
{
a[j]=str.buffer[i];
j++;
}
}
if(j==0)return 0;
else for(unsigned i=0;i<j;i++)
{
a[i]=a[i]-48;
int k=j;
for(;j>1+i;j--)
{
a[i]=a[i]*10;
}
j=k;
}
for(i=0;i<j;i++)
r+=a[i];
return r;
}
/////////////////////////////////////////////////////////////////////////
void string::replace(string &base,string pattern,string rp)
{
unsigned len=base.buflen,f=0;
base.buflen=(base.buflen+rp.buflen-pattern.buflen);
base=new char[base.buflen];
for(unsigned i=0;i<base.buflen;i++)
{
for(unsigned j=0;(base[i]=pattern[j])&&(j<pattern.buflen);i++,j++)
;
}
}
展开全部
typedef basic_string<char, char_traits<char>, allocator<char> >
string;
具体basic_string模板的代码相当多
你可以用visual c++中的“查看定义”功能来查看
祝学习进步!
string;
具体basic_string模板的代码相当多
你可以用visual c++中的“查看定义”功能来查看
祝学习进步!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
参考 高质量C++/C编程指南 林锐
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询