C++ String类简单实现

本文介绍了一个自定义字符串类MyString的设计与实现,包括构造函数、析构函数、重载运算符等核心功能,并实现了字符串连接、比较等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#ifndef MYSTRING_H
#define MYSTRING_H
#include<iostream>
using namespace std;
class MyString 
{

/*重载<<*/
    friend ostream& operator<<(ostream& _cout, MyString& _MyString)
    {
        _cout << _MyString.string_address;
        return _cout;
    }


/*重载>>*/
    friend istream& operator>>(istream& _cin, MyString& _MyString)
    {
        if (_MyString.string_address) {
            free(_MyString.string_address);
            _MyString.string_address = NULL;
        }

        _MyString.string_address = (char*)calloc(1, 1024);
        if (!_MyString.string_address)
            cout << "分配内存失败" << endl;

        _cin >> _MyString.string_address;
        return _cin;
    }

public:
/*默认无参构造*/
    MyString() :string_address(NULL) 
    {
    }


/*默认有参构造*/
    MyString(const char* const_string)
    {
        string_address = (char*)calloc(1, strlen(const_string) + 1);
        if (!string_address)
            cout << "内存分配失败" << endl;
        strcpy(string_address, const_string);
    }


/*拷贝构造*/
    MyString(const MyString& other_MyString)
    {
        string_address = (char*)calloc(1, strlen(other_MyString.string_address) + 1);
        if (!string_address)
            cout << "内存分配失败" << endl;
        strcpy(string_address, other_MyString.string_address);
    }


/*析构函数*/
    ~MyString()
    {
        if (string_address){
            free(string_address);
            string_address = NULL;
        }   
    }


/*普通成员函数*/
    const char* c_str() const
    {
        return string_address;
    }


/*获取字符串长度*/
    size_t length()
    {
        return strlen(string_address);
    }
    size_t size()
    {
        return strlen(string_address);
    }


/*重载=*/
    MyString& operator=(const MyString& _MyString)
    {
        if (string_address) {
            free(string_address);
            string_address = NULL;
        }
        string_address = (char*)calloc(1, strlen(_MyString.string_address) + 1);
        if (!string_address)
            cout << "内存分配失败" << endl;
        strcpy(string_address, _MyString.string_address);
        return *this;
    }
    MyString& operator=(const char* const_string)
    {
        if (string_address) {
            free(string_address);
            string_address = NULL;
        }
        string_address = (char*)calloc(1, strlen(const_string) + 1);
        if (!string_address)
            cout << "内存分配失败" << endl;
        strcpy(string_address, const_string);
        return *this;
    }


/*重载[]*/
    char& operator[](size_t index)
    {
        if (index >= length())
            return string_address[length() - 1];
        return string_address[index];
    }


/*重载+=*/
    MyString& operator+=(MyString& _MyString)
    {
        if (string_address){
            if (_MyString.string_address){
                string_address = (char*)realloc(string_address, length() + strlen(_MyString.string_address) + 1);
                if (!string_address) 
                    cout << "内存分配失败" << endl;
                strcat(string_address, _MyString.string_address);
            }

        }
        else {
            if (_MyString.string_address){
                string_address = (char*)calloc(1, strlen(_MyString.string_address + 1));
                if (!string_address)
                    cout << "内存分配失败" << endl;
                strcpy(string_address, _MyString.string_address);
            }
        }
        return *this;

    }
    MyString& operator+=(const char* const_string)
    {
        if (string_address) {
            if (const_string) {
                string_address = (char*)realloc(string_address,length() + strlen(const_string) + 1);
                if (!string_address)
                    cout << "内存分配失败" << endl;
                strcat(string_address, const_string);
            }
        }
        else {
            if (const_string) {
                string_address = (char*)calloc(1, strlen(const_string) + 1);
                if (!string_address)
                    cout << "内存分配失败" << endl;
                strcpy(string_address, const_string);
            }
        }
        return *this;
    }


/*重载+*/
    MyString operator+(MyString& _MyString)
    {
        if (string_address) {
            if (_MyString.string_address) {
                string_address = (char*)realloc(string_address, length() + _MyString.length() + 1);
                if (!string_address)
                    cout << "内存分配失败" << endl;
                strcat(string_address, _MyString.string_address);
            }
        }
        else {
            if (_MyString.string_address) {
                string_address = (char*)calloc(1, _MyString.length() + 1);
                if (!string_address)
                    cout << "内存分配失败" << endl;
                strcpy(string_address, _MyString.string_address);
            }
        }
        return *this;
    }
    MyString operator+(const char* const_string)
    {
        if (string_address) {
            if (const_string) {
                string_address = (char*)realloc(string_address, length() + strlen(const_string) + 1);
                if (!string_address)
                    cout << "内存分配失败" << endl;
                strcat(string_address, const_string);
            }
        }
        else {
            if (const_string) {
                string_address = (char*)calloc(1, strlen(const_string) + 1);
                if (!string_address)
                    cout << "内存分配失败" << endl;
                strcpy(string_address, const_string);
            }
        }
        return *this;
    }


/*重载 ==*/
    bool operator==(MyString& _MyString)
    {
        if (string_address)
            if (_MyString.string_address)
                if (!strcmp(string_address, _MyString.string_address))
                    return true;
        else
            if (!_MyString.string_address)
                return true;
        return false;
    }
    bool operator==(const char* const_string)
    {
        if (string_address)
            if (const_string)
                if (!strcmp(string_address, const_string))
                    return true;
                else
                    if (!const_string)
                        return true;
        return false;
    }


/*重载!=*/
    bool operator!=(MyString& _MyString)
    {
        if (string_address)
            if (_MyString.string_address)
                if (strcmp(string_address, _MyString.string_address))
                    return true;
                else
                    if (_MyString.string_address)
                        return true;
        return false;
    }
    bool operator!=(const char* const_string)
    {
        if (string_address)
            if (const_string)
                if (strcmp(string_address, const_string))
                    return true;
                else
                    if (const_string)
                        return true;
        return false;
    }


private:
    char* string_address;
};
#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值