【C++】String类的实现

1.String类中涉及到了拷贝构造
(1)浅拷贝
浅拷贝就是地址拷贝,拷贝之后与拷贝目标共用同一块空间
这里写图片描述

String(const String& s)
{
    if (s._pStr == NULL)
    {
        return;
    }
    _pStr = s._pStr;
}

浅拷贝存在的问题:
两个指针指向同一块空间,当释放空间时将会对同一块空间释放两次导致崩溃。

(2)深拷贝
深拷贝就是重新开辟一块与拷贝目标大小相同的空间,再把目标中的元素全都拷贝过来。

String(const String& s)
    {
        if (s._pStr == NULL)
        {
            return;
        }
        _pStr = new char[my_strlen(s._pStr) + 1];
        my_strcpy(_pStr,s._pStr);
    }

2.String类代码

#include<iostream>
#include<string.h>
#include<Windows.h>
using namespace std;

class String
{
public:
    String(char *pStr = "")    //构造函数
    {
        if (pStr == NULL)
        {
            _pStr = new char[1];
            *_pStr = '\0';
        }
        else
        {
            _pStr = new char[my_strlen(pStr) + 1];
            my_strcpy(_pStr, pStr);
        }
    }
    String(const String& s)    //拷贝构造函数
    {
        if (s._pStr == NULL)
        {
            return;
        }
        _pStr = new char[my_strlen(s._pStr) + 1];
        my_strcpy(_pStr,s._pStr);
    }

    ~String()                //析构函数
    {
        if (_pStr != NULL)
        {
            delete[]_pStr;
            _pStr = NULL;
        }
    }
    ///////////////////////字符串函数自定义
    int my_strlen(const char *p)
    {
        int count=0;
        while (*p != '\0')
        {
            count++;
            p++;
        }
        return count;
    }
    char* my_strcat(char *des, const char *src)
    {
        if (des == NULL)
        {
            cout << "参数错误" << endl;
            return 0;
        }
        char *ret = des;
        while (*des != '\0')
            des++;
        while (*des++ = *src++)
            ;
        return ret;
    }
    char* my_strcpy(char *des, const char *src)
    {
        if (des == NULL)
        {
            MessageBeep(0);
        }
        char *ret = des;
        while (*ret++ = *src++)
            ;
        return des;
    }
    int my_strcmp(const char*dest, const char*str)
    {
        while (*dest == *str)
        {
            dest++;
            str++;
        }
        if (*dest > *str)
            return 1;
        else if (*dest < *str)
            return -1;
        else
            return 0;

    }
    ///////////////////////////////////////
    void display()       //打印函数
    {
        cout << _pStr << endl;
    }
    // 不能使用库函数 
    String &operator=(const String& d);   //赋值符重载
    size_t Length();                    //字符串长度
    size_t Size();                     //字符串所占空间大小

    char& operator[](const size_t index);              //重载[]
    const char& operator[](const size_t index)const;   //与上一行函数成对出现
        bool operator>(const String& s);       //重载>
        bool operator<(const String& s);       //重载<
        bool operator==(const String& s);      //重载==
        bool operator!=(const String& s);      //重载!=
        bool strstr(const String& s);          //判断在一个对象是否包含另一对象
    String& operator+=(const String& s);        //重载+=
    String operator+(const String&s);          //重载+

private:
    char *_pStr;
};

String& String::operator=(const String&d)
{
    if (this != &d)
    {
        delete [] _pStr;
        _pStr = new char[my_strlen(_pStr) + 1];
        my_strcpy(_pStr,d._pStr);
    }
    return *this;
}

size_t String::Length()   //返回字符串长度
{
    return my_strlen(_pStr);
}
size_t String::Size()    //返回字符串所占空间大小
{
    return (Length() + 1);
}
char& String::operator[](const size_t index)
{
    return _pStr[index-1];
}
const char& String::operator[](const size_t index)const
{
    return _pStr[index - 1];
}
//////////////////
bool String::operator>(const String& s)
{
    return my_strcmp(s._pStr, _pStr) > 0 ? true : false;
}
bool String::operator<(const String& s)
{
    return my_strcmp(s._pStr, _pStr) < 0 ? true : false;
}
bool String::operator==(const String& s)
{
    return my_strcmp(s._pStr, _pStr) == 0 ? true : false;
}
bool String::operator!=(const String& s)
{
    return my_strcmp(s._pStr, _pStr) != 0 ? true : false;
}
bool String::strstr(const String& s)
{
    char*temp = s._pStr;
    while (*_pStr)
    {
        while ((*_pStr != *s._pStr)&&(*_pStr!='\0'))
            _pStr++;
        char*ret = _pStr;
        while ((*ret == *temp) && (*temp != '\0'))
        {
            ret++;
            temp++;
        }
        if (*temp == '\0')
            return true;
        else
            _pStr++;
    }
    return false;
}

String String::operator+(const String&s)
{
    String d;
    int len = my_strlen(_pStr) + my_strlen(s._pStr);
    d._pStr = new char[len + 1];
    my_strcpy(d._pStr,_pStr);
    my_strcat(d._pStr, s._pStr);
    return d;
}
String&  String::operator+=(const String& s)
{
    if (s._pStr == NULL)
        return *this;
    if (this == &s)
    {
        String copy(*this);
        return *this += copy;
    }
    int len = my_strlen(_pStr) + my_strlen(s._pStr);
    char *p_old = _pStr;
     _pStr = new char[len + 1];
    my_strcpy(_pStr,p_old);
    my_strcat(_pStr,s._pStr);
    delete []p_old;           
    return *this;
}

int main()
{
    String t1("world!");
    String t2("wormm!");
    cout << "t1 : ";
    t1.display();
    cout << "t2 : ";
    t2.display();


    bool judge = (t1 > t2);
    bool judge2 = (t1 < t2);
    bool judge3 = (t1 == t2);
    bool judge4 = (t1 != t2);
    cout << judge << endl;
    cout << judge2 << endl;
    cout << judge3 << endl;
    cout << judge4 << endl;

    String t3 = t1 + t2;
    t1 += t2;
    cout << "t3 : ";
    t3.display();
    cout << "t1 : ";
    t1.display();

    String t4("worlsadfdjkasd!");
    String t5("fhdj");
    bool temp =t4.strstr(t5);
    cout << temp << endl;

    system("pause");
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值