2024.9.3C++

自行实现Mystring类

#include <iostream>
#include <cstring>
using namespace std;

class mystring
{
public:
    mystring()
    {
        len = 0;
        str = nullptr;
    }
    mystring(const char* s)
    {
        len = strlen(s);
        str = new char[len + 1];
        strcpy(str, s);
    }
    mystring(const mystring& other)
    {
        len = other.len;
        str = new char[len + 1];
        strcpy(str, other.str);
    }
    ~mystring()
    {
        delete[] str;
    }
    mystring& operator=(const mystring& other)
    {
        if (this != &other)
        {
            len = other.len;
            delete[] str;
            str = new char[len + 1];
            strcpy(str, other.str);
        }
        return *this;
    }
    mystring operator+(const mystring& other)
    {
        mystring s;
        s.len = len + other.len;
        s.str = new char[s.len + 1];
        strcpy(s.str, str);
        strcat(s.str, other.str);
        return s;
    }

    bool operator!=(const mystring& other)
    {
        if(strcmp(str, other.str)!= 0)
        {
            return true;
        }
        return false;

    }
    bool operator==(const mystring& other)
    {
        if(strcmp(str, other.str) == 0)
        {
            return true;
        }
        return false;
    }
    bool operator<(const mystring& other)
    {
        if(strcmp(str, other.str) < 0)
        {
            return true;
        }
        return false;
    }
    bool operator>(const mystring& other)
    {
        if(strcmp(str, other.str) > 0)
        {
            return true;
        }
        return false;
    }
    bool operator<=(const mystring& other)
    {
        if(strcmp(str, other.str) <= 0)
        {
            return true;
        }
        return false;
    }
    bool operator>=(const mystring& other)
    {
        if(strcmp(str, other.str) >= 0)
        {
            return true;
        }
        return false;
    }

    const char* data() const
    {
        return str;
    }
    bool empty() const
    {
        return len == 0;
    }
    int size() const
    {
        return len;
    }
    int length() const
    {
        return len;
    }
    char at(int index) const
    {
        if (index < 0 || index >= len)
        {
            throw out_of_range("Index out of range");
        }
        return str[index];
    }
    friend ostream &operator<<(ostream& L, const mystring &R);
    friend istream &operator>>(istream& L, mystring &R);
private:
    char* str;
    int len;
};
ostream& operator<<(ostream& L, const mystring &R)
{
    L << R.str<<endl;
    return L;
}

istream& operator>>(istream& L,mystring &R)
{
    L >> R.str;
    return L;
}

int main()
{
    mystring s1("hello");
    mystring s2(s1);
    cout << s1.data() << endl;
    cout << s2.data() << endl;
    s2 = mystring("world");
    cout << s1.data() << endl;
    cout << s2.data() << endl;
    cout << s1.size() << endl;
    cout << s2.size() << endl;
    cout << s1.empty() << endl;
    cout << s2.empty() << endl;
    cout << s1.length() << endl;
    cout << s2.length() << endl;
    try {
        cout << s1.at(0) << endl;
        cout << s2.at(0) << endl;
    } catch (const out_of_range& e) {
        cerr << e.what() << endl;
    }
    s1 = s1 + s2;
    cout << s1.data() << endl;
    cout << s2.data() << endl;
    cout << (s1 == s2) << endl;
    cout << (s1 != s2) << endl;
    cout << (s1 < s2) << endl;
    cout << (s1 > s2) << endl;
    cout << (s1 <= s2) << endl;
    cout << (s1 >= s2) << endl; 
    cout << s1 << endl;
    cin >> s1;
    cout << s1 << endl;

    return 0;
}

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值