c++中用字符数组实现string,重写+、+=、()、<、==、>、[]运算符———全面的MyString

#include <iostream>
#include <cstring>

using namespace std;

class MyString {
    // 在此处补充你的代码
    size_t len;
    char *str = nullptr;
public:
    MyString(const char *s) {
        if (s == nullptr)
            return;
        len = strlen(s);
        str = new char[len + 1];//初始化str
        strcpy(str, s);
        str[strlen(s)] = '\0';//一定要在最后加'\0'
    }

    MyString() {
        len = 0;
        str = nullptr;
    }

    ~MyString() {
        //一定得判断str是否是空,要不删除一个空指针应该是不行的吧
        if (str)
            delete (str);
    }

    //复制构造函数
    MyString(const MyString &mys) {
        if (mys.str == nullptr)
            return;
        else {
            len = strlen(mys.str);
            str = new char[len + 1];
            strcpy(str, mys.str);//复制函数很重要
        }
    }

//为什么不加friend只能有一个参数,而加了后就可以有两个参数了?
//因为对cout<<1;这句话,实际是operator<<(cout,1),也就是说<<本来是需要两个参数的。如果把<<重载成一个类的成员函数,则参数减1,其中的一个参数就是类对象本身。加了friend后该函数就不是成员函数了,所以要有两个成员函数
    friend ostream &operator<<(ostream &cout, const MyString &s) {
        if (s.str != nullptr)
            cout << s.str;
        return cout;
    }

    MyString &operator=(const MyString &mys) {
        if (mys.str == str)
            return *this;
        else {
            //先把原来的空间给清除掉
            if (str)
                delete[]str;
            len = strlen(mys.str);
            str = new char[len + 1];
            strcpy(str, mys.str);
        }
        return *this;
    }

    //这个+和+=的函数主体不是一样的吗??
    MyString operator+(const MyString &mys) {
        //这个判断必须要有吧。。
        if (mys.str == nullptr)
            return *this;
        MyString tempmys;
        tempmys.len = len + strlen(mys.str);
        tempmys.str = new char[tempmys.len + 1];
        //先把原来的赋值给strtemp
        strcpy(tempmys.str, str);
        strcat(tempmys.str, mys.str);//拼接
        return tempmys;
    }

    //bug 2 注意顺序。。。这不是加减法!!!重载的成意思是字符串的拼接,所以*this在前面,s在后面
    MyString operator+(const char *s) {
        MyString mys(s);
        return *this + mys;
    }

    friend MyString operator+(const char *s, const MyString &rhs) {
        MyString mys(s);
        return mys + rhs;
    }

    //bug 3 注意,+=返回的是引用,因为mys+="abc"这句话,相当于是mys.operator(+=)("abc"),要想作用到mys上,必须返回当前对象*this的引用
    MyString &operator+=(const char *s) {
        MyString mys(s);
        *this = *this + mys;
        return *this;
    }

    //这里不能返回数组,因为这样的话,自己要new出一个空间复制原来的数据,但是new后不会再自己释放,它不是类,不会造成内存泄露!!!!
    //但是这样又会存在一个问题,即如果我想通过这样的下标修改某一块的值时不行,因为是两块内存
    MyString operator()(int i, int j) {
        MyString mys;
        mys.len = j;
        char *temps = new char[j + 1];
        int start = 0;
        for (start = 0; start < j; start++)
            temps[start] = str[i + start];
        temps[start] = 0;
        mys.str = temps;
        return mys;
    }

    int operator<(const MyString &rhs) {
        if (strcmp(str, rhs.str) < 0)
            return true;
        else
            return false;
    }

    int operator==(const MyString &rhs) {
        if (strcmp(str, rhs.str) == 0)
            return true;
        else
            return false;
    }

    int operator>(const MyString &rhs) {
        if (strcmp(str, rhs.str) > 0)
            return true;
        else
            return false;
    }

    //这里返回值要为引用
    char &operator[](int i) {
        return str[i];
    }
};


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);
    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;
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值