📄 string1.h
字号:
#ifndef String1_hpp
#define String1_hpp
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
const long maxLen=256;
class String{
public:
String(const String &ob);//
//String(const char *init);
String();
~String(){delete []ch;}
int Length()const{return curLen;}
/*String &operator()(int pos,int len);
int operator==(const String &ob)const{return strcmp(ch,ob.ch)==0;}
int operator==(const char *ob)const{return strcmp(ch,ob)==0;}
int operator!=(const String &ob)const{return strcmp(ch,ob.ch)!=0;}
int operator!=(const char *ob)const{return strcmp(ch,ob)!=0;}
int operator!()const{return curLen==0;}
String &operator=(const String &ob);
String &operator+=(const String &ob);*/
String &operator+=(unsigned const char &ob);//
unsigned char &operator[](int i);//
//int Find(String &pat)const;
friend istream& operator>>(istream &is,String &s);
friend ostream& operator<<(ostream &os,const String &s);
private:
int curLen;
unsigned char *ch;//
};
String::String(const String &ob){
ch=new unsigned char[maxLen+1];unsigned char c;//
if(!ch){cerr<<"Allocation Error\n";exit(1);}
curLen=ob.curLen;
for(int i=0;i<ob.Length();i++){
c=ob.ch[i];
operator+=(c);
}
//strcpy(ch,ob.ch);
}/**/
/*String::String(const char *init){
ch=new char[maxLen+1];
if(!ch){cerr<<"Allocation Error\n";exit(1);}
curLen=strlen(init);
strcpy(ch,init);
}*/
String::String(){
ch=new unsigned char[maxLen+1];//
if(!ch){cerr<<"Allocation Error\n";exit(1);}
curLen=0;
ch[0]='\0';
}
/*String &String::operator()(int pos,int len){
String *temp=new String;
if(pos<0||pos+len-1>=maxLen||len<0){
temp->curLen=0;temp->ch[0]='\0';
}
else{
if(pos+len-1>=curLen)len=curLen-pos;
temp->curLen=len;
for(int i=0,j=pos;i<len;i++,j++)temp->ch[i]=ch[j];
temp->ch[len]='\0';
}
return *temp;
}
String &String::operator=(const String &ob){
if(&ob!=this){
delete []ch;
ch=new char[maxLen+1];
if(!ch){cerr<<"Out Of Memory!\n";exit(1);}
curLen=ob.curLen;
strcpy(ch,ob.ch);
}
else cout<<"Attempted assignment of a String to itselt!\n";
return *this;
}*/
String &String::operator+=(const unsigned char &ob){
ch[curLen]=ob;
ch[++curLen]='\0';
return *this;
}
/*String &String::operator+=(const String &ob){
char *temp=ch;
curLen+=ob.curLen;
ch=new char[maxLen+1];
if(!ch){cerr<<"Out Of Memory\n";exit(1);}
strcpy(ch,temp);
strcpy(ch,ob.ch);
strcat(ch,ob.ch);
delete []temp;return *this;
}*/
unsigned char &String::operator[](int i){//
if(i<0&&i>=curLen){cout<<"Out Of Boundary\n";exit(1);}
return ch[i];
}
istream& operator>>(istream &is,String &s){
char c;s.curLen=0;
is>>c;
while(c!='\r'){
s.ch[s.curLen]=c;
s.curLen++;
}
return is;
}
ostream& operator<<(ostream &os,const String &s){
for(int i=0;i<s.Length();i++)
os<<s.ch[i];
return os;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -