字符串的重载程序

本文介绍了一个名为MyString的自定义字符串类的设计与实现细节,包括构造函数、析构函数、赋值运算符、比较运算符等重载,并演示了如何使用该类进行字符串操作。

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

MyString.h

#ifndef __MYSTRING_H__
#define __MYSTRING_H__

#include <iostream>

using namespace std;

class MyString
{
    // 重载 << 操作符
    friend std::ostream& operator<<(std::ostream &out, MyString &str);

    // 重载 >> 操作符
    friend std::istream& operator>>(std::istream& in, MyString &str);
public:
    MyString();                        // 无参构造
    MyString(const char *s);           // 有参构造
    MyString(int len, char data = 0);  // 有参构造
    MyString(const MyString &s);       // 拷贝构造

    ~MyString();                       // 析构函数

// 重载=、[]操作符
public:
    MyString& operator=(const char *s);       // 普通字符串赋值
    MyString& operator=(const MyString &s);   // 类对象之间赋值
    char & operator[](int index);

// 重载 + 运算符
public:
    MyString& operator+(const char *str);
    MyString& operator+(const MyString &s);

    MyString& operator+=(const char *str);
    MyString& operator+=(const MyString &s);

// 重载 == !=
public:
    bool operator==(const char *str) const;
    bool operator==(const MyString &str) const;

    bool operator!=(const char *str) const;
    bool operator!=(const MyString &str) const;

// 重载 < >
public:
    bool operator>(const char *str) const;
    bool operator>(const MyString &str) const;

    bool operator<(const char *str) const;
    bool operator<(const MyString &str) const;

public:
    const char *c_str()
    {
        return m_p;
    }

    char *c_str2()
    {
        return m_p;
    }

    int leng()
    {
        return m_len;
    }
private:
    int m_len;    // 字符串长度
    char *m_p;    // 字符串数据
};




#endif // __MYSTRING_H__

MyString.cpp

#include "MyString.h"
#include <string.h>

std::ostream& operator<<(std::ostream &out, MyString &str)
{
    out << str.m_p << " ";

    return out;
}

std::istream& operator>>(std::istream& in, MyString &str)
{
    in >> str.m_p;
    //fgets (str.m_p, str.m_len, stdin);
    return in;
}

MyString::MyString()
{
    m_len = 0;
    m_p = new char[1];
}

MyString::MyString(const char *s)
{
    if (s == NULL)
    {
        m_len = 0;
        m_p = new char[1];
    }
    else
    {
        m_len = strlen(s);
        m_p = new char[m_len + 1];
        strcpy (m_p, s);
    }
}

MyString::MyString(int len, char data)
{
    m_len = len;
    m_p = new char[m_len+1];
    for (int i = 0; i < m_len; i++)
    {
        m_p[i] = data;
    }
}


MyString::MyString(const MyString &s)
{
    m_len = s.m_len;
    m_p = new char[m_len+1];
    strcpy (m_p, s.m_p);
}


MyString::~MyString()
{
    delete[] m_p;
    m_p = NULL;
    m_len = 0;
}


MyString& MyString::operator=(const char *s)
{
    // 先释放原有空间
    delete [] m_p;
    if (s == NULL)
    {
        m_len = 0;
        m_p = new char[1];
    }
    else
    {
        m_len = strlen(s);
        m_p = new char[m_len+1];
        strcpy(m_p, s);
    }

    return *this;
}

MyString& MyString::operator=(const MyString &s)
{
    // 是本身的情况下,直接返回当前对象
    if (this == &s)
        return *this;

    // 先释放
    delete[] m_p;

    m_len = s.m_len;
    m_p = new char[m_len + 1];
    strcpy (m_p, s.m_p);

    return *this;
}

MyString& MyString::operator+(const char *str)
{
    if (str == NULL)
    {
        return *this;
    }

    m_len += strlen(str);
    char *tmp = m_p;            // 将原有数据保存下来
    m_p = new char[m_len + 1];  // 分配新空间
    strcpy (m_p, tmp);          // 将原来的数据导入到新空间
    strcat (m_p, str);          // 将新数据粘到原有数据后面
    delete [] tmp;              // 释放原来的空间

    return *this;
}

MyString& MyString::operator+(const MyString &s)
{
    m_len += s.m_len;
    char *tmp = m_p;            // 将原有数据保存下来
    m_p = new char[m_len + 1];  // 分配新空间
    strcpy (m_p, tmp);          // 将原来的数据导入到新空间

    if (this == &s)             // 如果是自己,复制原有空间内容
        strcat (m_p, tmp);
    else                        // 复制新字符串的内容
        strcat (m_p, s.m_p);

    delete [] tmp;              // 释放原来的空间

    return *this;
}

MyString& MyString::operator+=(const char *str)
{
    *this = *this + str;

    return *this;
}

MyString& MyString::operator+=(const MyString &s)
{
    *this = *this + s;
    return *this;
}

char & MyString::operator[](int index)
{
    return m_p[index];
}

bool MyString::operator==(const char *str) const
{
    if (str == NULL)
    {
        if (m_len == 0)
            return true;
        else
            return false;
    }
    else
    {
        if (strcmp(m_p, str) == 0)
            return true;
        else
            return false;
    }
}

bool MyString::operator==(const MyString &str) const
{
    return *this == str.m_p;
}

bool MyString::operator!=(const char* str) const
{
    return !(*this == str);
}

bool MyString::operator!=(const MyString &str) const
{
    return !(*this == str.m_p);
}

bool MyString::operator>(const char *str) const
{
    if(str == NULL)
        return true;

    int i = 0;
    int len = (m_len < strlen(str) ? m_len : strlen(str)); // 保存较短的字符串的长度
    while (m_p[i] == str[i] && i<len)
        i++;

    return (m_p[i] > str[i] ? true : false);
}

bool MyString::operator>(const MyString &str) const
{
    return *this > str.m_p;
}

bool MyString::operator<(const char *str) const
{
    if(str == NULL)
        return true;

    int i = 0;
    int len = (m_len < strlen(str) ? m_len : strlen(str)); // 保存较短的字符串的长度
    while (m_p[i] == str[i] && i<len)
        i++;

    return (m_p[i] < str[i] ? true : false);
}

bool MyString::operator<(const MyString &str) const
{
    return *this < str.m_p;
}

main.cpp

#include <iostream>
#include "MyString.h"

int main()
{
    MyString str = "hello";
    char * p = str.c_str2();

    printf ("%s\n", str.c_str());

    return 0;
}


int main7()
{
    MyString str1 = "chelo";
    MyString str2 = "world";

    if (str1 > "bhello")
    {
        printf ("str1 > hello\n");
    }

    if (str1 > str2)
    {
        printf ("str1 > str2\n");
    }
    else
    {
        printf ("str1 < str2\n");
    }

    return 0;
}



int main6()
{
    MyString str1 = "helo";
    MyString str2 = "world";

    if (str1 == "hello")
    {
        printf ("str1 == hello\n");
    }
    else
    {
        printf ("str1 != hello\n");
    }

    if (str1 == str2)
    {
        printf ("str1 == str2\n");
    }
    else
    {
        printf ("str1 != str2\n");
    }

    return 0;
}

int main5()
{
    MyString str1 = "hello";

    std::cout << str1[3] << std::endl;
    str1[3] = 'w';
    std::cout << str1 << std::endl;

    return 0;
}

int main4()
{
    MyString str1 = "hello ";
    MyString str2 = "world";

    str1 = str1 + "borld";  
    // str1 = str1 + str1;
    // str1 += "world";
    //str1 += str2;

    std::cout << str1 << std::endl;

    return 0;
}

int main3()
{
    MyString str1;

    // MyString & operator(MyString &str, const char *s)
    str1 = "hello";   // 重载赋值操作符 = 
    //str1 = NULL;
    std::cout << str1 << std::endl;

    MyString str2;
    str2 = str1;
    std::cout << str2 << std::endl;

    str2 = str2;

    return 0;
}

int main2()
{
    MyString str1 = "hello world";   // 用一个字串去初始化类对象

    // << 
    // ostream & operator<<(ostream &out, MyString &str)
    std::cout << str1 << std::endl;

    MyString str2(4);

    // >> 
    // istream& operator>>(istream& in, MyString &str);
    std::cout << "请输入字符串:" ;
    std::cin >> str2;
    std::cout << str2 << std::endl;

    return 0;
}


int main8()
{
    MyString str;                    // 空字符串
    MyString str1 = "hello world";   // 用一个字串去初始化类对象
    cout << str1 << endl;
    MyString str2 = NULL;            // 空字符串
    MyString str3 = str1;            // 用一个字符串对象初始化当前对象
    MyString str4(10, 'a');           // 长度为10,每一个元素都是'a'
    cout << str4 << endl;
    MyString str5(20); 

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值