20240903mystring进阶

#include <iostream>
#include <cstring>
#include <stdexcept>  // For std::out_of_range

class Mystring
{
public:
    // 默认构造函数
    Mystring() : str(nullptr), len(0) {}

    // 有参构造函数
    Mystring(const char* s)
    {
        len = strlen(s);
        str = new char[len + 1];  // 分配空间,并多留一个位置给'\0'
        strcpy(str, s);
    }

    // 拷贝构造函数
    Mystring(const Mystring& other)
    {
        len = other.len;
        str = new char[len + 1];
        strcpy(str, other.str);
    }

    // 拷贝赋值操作符
    Mystring& operator=(const Mystring& other)
    {
        if (this == &other)
        {
            return *this;  // 检查自赋值
        }

        delete[] str;  // 删除旧的字符串数据

        len = other.len;
        str = new char[len + 1];
        strcpy(str, other.str);

        return *this;
    }

    // 析构函数
    ~Mystring()
    {
        delete[] str;
    }

    // 获取字符串的C风格表示
    const char* c_str() const
    {
        return str;
    }

    // 获取字符串长度
    int size() const
    {
        return len;
    }

    // 获取字符串长度
    int length() const
    {
        return len;
    }

    // 判断字符串是否为空
    bool empty() const
    {
        return len == 0;
    }

    // 获取字符串中某个位置的字符
    char& at(int index)
    {
        if (index < 0 || index >= len)
        {
            throw std::out_of_range("Index out of range");
        }
        return str[index];
    }

    // 常量版本的at函数
    const char& at(int index) const
    {
        if (index < 0 || index >= len)
        {
            throw std::out_of_range("Index out of range");
        }
        return str[index];
    }

    // 重载 + 运算符,用于连接两个 Mystring 对象
    Mystring operator+(const Mystring& other) const
    {
        int new_len = len + other.len;
        char* new_str = new char[new_len + 1];
        strcpy(new_str, str);
        strcat(new_str, other.str);

        Mystring temp;
        temp.str = new_str;
        temp.len = new_len;

        return temp;
    }

    // 重载 == 运算符
    bool operator==(const Mystring& other) const
    {
        return strcmp(str, other.str) == 0;
    }

    // 重载 != 运算符
    bool operator!=(const Mystring& other) const
    {
        return !(*this == other);
    }

    // 重载 < 运算符
    bool operator<(const Mystring& other) const
    {
        return strcmp(str, other.str) < 0;
    }

    // 重载 <= 运算符
    bool operator<=(const Mystring& other) const
    {
        return strcmp(str, other.str) <= 0;
    }

    // 重载 > 运算符
    bool operator>(const Mystring& other) const
    {
        return strcmp(str, other.str) > 0;
    }

    // 重载 >= 运算符
    bool operator>=(const Mystring& other) const
    {
        return strcmp(str, other.str) >= 0;
    }

    // 模板函数:插入到输出流
    template<typename T>
    friend std::ostream& operator<<(std::ostream& os, const Mystring& obj);

    // 模板函数:从输入流提取字符串
    template<typename T>
    friend std::istream& operator>>(std::istream& is, Mystring& obj);

private:
    char* str;  // 指向字符串的指针
    int len;    // 字符串长度
};

// 模板函数:插入到输出流
template<typename T>
std::ostream& operator<<(std::ostream& os, const Mystring& obj)
{
    os << obj.c_str();
    return os;
}

// 模板函数:从输入流提取字符串
template<typename T>
std::istream& operator>>(std::istream& is, Mystring& obj)
{
    // 读取输入流中的内容
    char buffer[1024];  // 假设最大输入长度为1023字符
    is.getline(buffer, 1024);

    // 重新分配内存并复制数据
    obj.len = strlen(buffer);
    delete[] obj.str;  // 删除旧的字符串数据
    obj.str = new char[obj.len + 1];
    strcpy(obj.str, buffer);

    return is;
}

int main() {
    Mystring s1("Hello");
    Mystring s2("World");
    Mystring s3;

    s3 = s1 + s2;  // 使用 + 运算符连接字符串

    std::cout << "s1: " << s1 << ", size: " << s1.size() << std::endl;//测试输出
    std::cout << "s2: " << s2 << ", size: " << s2.size() << std::endl;//
    std::cout << "s3 (s1 + s2): " << s3 << ", size: " << s3.size() << std::endl;

    std::cout << "s1 == s2? " << (s1 == s2 ? "Yes" : "No") << std::endl;
    std::cout << "s1 != s2? " << (s1 != s2 ? "Yes" : "No") << std::endl;
    std::cout << "s1 < s2? " << (s1 < s2 ? "Yes" : "No") << std::endl;
    std::cout << "s1 <= s2? " << (s1 <= s2 ? "Yes" : "No") << std::endl;
    std::cout << "s1 > s2? " << (s1 > s2 ? "Yes" : "No") << std::endl;
    std::cout << "s1 >= s2? " << (s1 >= s2 ? "Yes" : "No") << std::endl;

    std::cout << "Enter a new string for s3: ";
    std::cin >> s3;
    std::cout << "Updated s3: " << s3 << std::endl;

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值