PSVM_KEN
2020-04-20 18:12
浏览 353

为啥在DEVC++能运行,VS2019却报错?(来源poj北大郭炜老师:全面的MyString)

#include
#include
using namespace std;
int strlen(const char * s) //求长度
{ int i = 0;
for(; s[i]; ++i);
return i;
}
void strcpy(char * d,const char * s) //复制字符串并将最后一个字符变成‘\0’
{
int i = 0;
for( i = 0; s[i]; ++i)
d[i] = s[i];
d[i] = 0;

}
int strcmp(const char * s1,const char * s2) //比较较短长度中每个字符大小
{
for(int i = 0; s1[i] && s2[i] ; ++i) {
if( s1[i] < s2[i] )
return -1;
else if( s1[i] > s2[i])
return 1;
}
return 0;
}
void strcat(char * d,const char * s) //连接字符串
{
int len = strlen(d);
strcpy(d+len,s);
}
class MyString
{
// 在此处补充你的代码
char *ptr;
public:
MyString(char *p){
ptr = new char[strlen(p)+1];
strcpy(ptr,p);
}
MyString(){ //数组不要定义为NULL
ptr = new char[2];
ptr[0] = 0;
}
MyString (const MyString &r){
if(r.ptr==NULL) return;
// if(ptr) delete []ptr;
ptr = new char[strlen(r.ptr)+1];
strcpy(ptr,r.ptr);
}
MyString & operator =(char *p){
if(ptr) delete []ptr;
ptr = new char[strlen(p)+1];
strcpy(ptr,p);
return *this;
}
friend ostream & operator <<( ostream &os,const MyString &r){
os<<r.ptr;
return os;
}
MyString & operator =(const MyString &r){
if(ptr==r.ptr) return *this;
if(ptr) delete []ptr;
ptr = new char[strlen(r.ptr)+1];
strcpy(ptr,r.ptr);
return *this;
}
MyString operator +(const MyString &r){ //不加const 会报错
char *tmp = new char[strlen(ptr)+strlen(r.ptr)+2]; //strlen()不包括'\0',但strcat()和strcpy()包括'\0'
strcpy(tmp,ptr);
strcat(tmp,r.ptr);
MyString t(tmp);
delete []tmp;
return t;
}
char & operator {
return ptr[i];
}
MyString & operator +=(const char *p){
char *tmp = new char[strlen(ptr)+strlen(p)+2];
strcpy(tmp,ptr);
strcat(tmp,p);
delete []ptr;
ptr = tmp;
return *this;
}
friend MyString operator +(const char *p,const MyString &r){
char *tp = new char[strlen(p)+strlen(r.ptr)+2];
strcpy(tp,p);
strcat(tp,r.ptr);
MyString tmp(tp);
delete []tp;
return tmp;
}

    MyString operator ()(int start,int len){
        char *p = new char[len+1];
        for(int i=0;i<len;++i)
           p[i] = ptr[i+start];
           p[len] = 0;
        MyString tmp(p);
        delete []p;
        return tmp;
    }
    friend bool operator <(const MyString &r1,const MyString &r2){
        return strcmp(r1.ptr,r2.ptr) <0;
    }
    friend bool operator >(const MyString &r1,const MyString &r2){
        return strcmp(r1.ptr,r2.ptr) >0;
    }
    friend bool operator ==(const MyString &r1,const MyString &r2){
        return strcmp(r1.ptr,r2.ptr) ==0;
    }

    ~MyString(){
        if(ptr) delete []ptr;
    }

};

int CompareString( const void * e1, const void * e2) //比较字符串大小
{
MyString * s1 = (MyString * ) e1;
MyString * s2 = (MyString * ) e2;
if( * s1 < *s2 ) //重载< > ==
return -1;
else if( *s1 == *s2)
return 0;
else if( *s1 > *s2 )
return 1;
}
int main()
{
MyString s1("abcd-"),s2,s3("efgh-"),s4(s1); //构造函数、复制构造函数
MyString SArray[4] = {"big","me","about","take"}; //“=”重载(字符换和对象)
cout << "1. " << s1 << s2 << s3<< s4<< endl; //“<<”重载
s4 = s3; //“=”重载 (对象与对象)
s3 = s1 + s3; //“+”重载
cout << "2. " << s1 << endl;
cout << "3. " << s2 << endl;
cout << "4. " << s3 << endl;
cout << "5. " << s4 << endl;
cout << "6. " << s1[2] << endl; //“[]”重载
s2 = s1;
s1 = "ijkl-";
s1[2] = 'A' ;
cout << "7. " << s2 << endl;
cout << "8. " << s1 << endl;
s1 += "mnop"; //“+=”重载
cout << "9. " << s1 << endl;
s4 = "qrst-" + s2; //"+"重载
cout << "10. " << s4 << endl;
s1 = s2 + s4 + " uvw " + "xyz";
cout << "11. " << s1 << endl;
qsort(SArray,4,sizeof(MyString),CompareString); //qsort函数 ->系统自带
for( int i = 0;i < 4;i ++ )
cout << SArray[i] << endl;
//s1的从下标0开始长度为4的子串
cout << s1(0,4) << endl; //()的重载
//s1的从下标5开始长度为10的子串
cout << s1(5,10) << endl;
return 0;
}


  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

相关推荐 更多相似问题