📄 字符串类.cpp
字号:
/*字符串类*/
#include<iostream>
#include<cstdlib>
using namespace std;
namespace std
{ //自定义名字空间
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;
}
//串拼接运算符
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()
{
curlen=1;
}
}; //类定义结束
String::String(const String&ob)
{
curlen=ob.curlen;
ch=new char[curlen];
if(ch==NULL)
{
cerr<<"allocation error!\n";
exit(1);
}
strcpy(ch,ob.ch);
}
String::String(char *init)
{
curlen=strlen(init)+1;//长度包括NULL字符
ch=new char[curlen];
if(ch==NULL)
{
cerr<<"allocation error!\n";
exit(1);
}
strcpy(ch,init);
}
String& String:: operator=(const String&ob)
{
//若大小不等则删除当前串,并重新申请内存
if(ob.curlen!=curlen)
{
delete[] ch;
ch=new char[ob.curlen];
if(ch==NULL)
{
cerr<<"allocation error!\n";
exit(1);
}
curlen=ob.curlen;
}
strcpy(ch,ob.ch);
return *this;
}
String& String::operator=(char*s)
{
if(strlen(s)+1!=curlen)
{
delete []ch;
ch=new char[strlen(s)+1];
if(ch==NULL)
{
cerr<<"allocation error!\n";
exit(1);
}
curlen=strlen(s)+1;
}
strcpy(ch,s);
return *this;
}
String String:: operator+(const String& ob) const
{
//在temp中建立一个长度为len的新串
String temp;
int len;
//删除定义temp是产生的NULL串
delete []temp.ch;
//计算拼接后的串长度并且为之申请内存
len=curlen+ob.curlen-1;//只有一个NULL结尾
temp.ch=new char[len];
if(temp.ch==NULL)
{
cerr<<"allocation error!\n";
exit(1);
}
//建立新串
temp.curlen=len;
strcpy(temp.ch,ch);
strcat(temp.ch,ob.ch);
return temp;
}
String String::operator+(char *s) const
{
String temp;
int len;
delete []temp.ch;
len=curlen+strlen(s);
temp.ch=new char[len];
if(temp.ch==NULL)
{
cerr<<"allocation error!\n";
exit(1);
}
temp.curlen=len;
strcpy(temp.ch,ch);
strcat(temp.ch,s);
return temp;
}
String operator+(char *s,const String & ob)
{
String temp;
int len;
delete []temp.ch;
len=ob.curlen+strlen(s);
temp.ch=new char[len];
if(temp.ch==NULL)
{
cerr<<"allocation error!\n";
exit(1);
}
temp.curlen=len;
strcpy(temp.ch,s);
strcat(temp.ch,ob.ch);
return temp;
}
String& String::operator+=(const String& ob)
{
char*temp=ch;
curlen=curlen+ob.curlen-1;
ch=new char[curlen];
if(ch==NULL)
{
cerr<<"allocation error!\n";
exit(1);
}
strcpy(ch,temp);
strcat(ch,ob.ch);
delete []temp;
return *this;
}
String &String::operator+=(char *s)
{
char*temp=ch;
curlen+=strlen(s);
ch=new char[curlen];
if(ch==NULL)
{
cerr<<"allocation error!\n";
exit(1);
}
strcpy(ch,temp);
strcat(ch,s);
delete []temp;
return *this;
}
int String::find(char c,int start)const
{
int ret;
char*p;
p=strchr(ch+start,c);
if(p!=NULL)
ret=int(p-ch);
else
ret=-1;
return ret;
}
int String::findlast(char c)const
{
int ret;
char *p;
p=strrchr(ch,c);
if(p!=NULL)
ret=int(p-ch);
else
ret=-1;
return ret;
}
String String::substr(int index,int count) const
{
//从index到串尾的字符个数
int charsleft=curlen-index-1,i;
//建立子串temp
String temp;
char*p,*q;
//若index越界.返回空串
if(index>=curlen-1)
return temp;
//若count大于剩下的字符,则只用剩下的字符
if(count>charsleft)
count=charsleft;
delete []temp.ch;
temp.ch=new char[count+1];
if(temp.ch==NULL)
{
cerr<<"allocation error!\n";
exit(1);
}
//从ch中拷贝count个字符到 temp.ch
for(i=0,p=temp.ch,q=&ch[index];i<count;i++)
*p++=*q++;
*p=0;
temp.curlen=count+1;
return temp;
}
char &String::operator[](int i)
{
return ch[i];
}
ostream &operator<<(ostream &ostr,const String& s)
{
cout<<s.ch;
return ostr;
}
istream &operator>>(istream &istr,String& s)
{
s.ch=new char[maxlen];
if(s.ch==NULL)
{
cerr<<"allocation error!\n";
exit(1);
}
istr.getline(s.ch,maxlen,'\n');
return istr;
}
}//定义名字空间结束
void main()
{
String s1("String"),s2("hello");
String s3,s4,s5;
char c;
s3=s1+" "+s2;
cout<<s3<<endl;
cout<<"the length of s2:"<<s2.length()<<endl;
cout<<"s2中第一个s的位置是:"<<s2.find('s',0)<<endl;
cout<<"s2中最后一个s的位置是:"<<s2.findlast('s')<<endl;
cout<<"s2中第一个空格的位置是:"<<s2.find(' ',0)<<endl;
cout<<"s2中最后一个空格的位置是:"<<s2.findlast(' ')<<endl;
cout<<"input s4:";
cin>>s4;cout<<s4;
s1="how are you?";
for(int i=0;i<s1.length();i++)
{
c=s1[i];
if(c>='A'&&c<='Z')
{
c+=32;
s1[i]=c;
}
}
cout<<"s1 is:"<<s1<<endl;
s1="ABCDE";s2="BCF";
int b=s2<s1;
cout<<b;
String as="0",f=s1;
String data("300");
data[0]='5';
cout<<data<<" ";
cout<<data.strempty()<<" ";
data.clear();
cout<<data.strempty()<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -