您当前的位置: 首页 >  c++

令狐掌门

暂无认证

  • 0浏览

    0关注

    513博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C++实现string类

令狐掌门 发布时间:2021-04-13 19:19:24 ,浏览量:0

    C++写一个string类,其实更多的是对C++内存分配,运算符重载的功能的运用。

    MyString.h

#pragma once

#include 
using namespace std;

class MyString
{
public:
    MyString();                           //默认构造函数
    MyString(int n, char c);              //普通构造函数
    MyString(const char* source);         //普通构造函数
    MyString(const MyString& s);          //复制构造函数
    MyString& operator=(char* s);         //重载=,实现字符串赋值
    MyString& operator=(const MyString& s);    //重载=,实现对象赋值

    ~MyString();                         //析构函数

    char& operator[](int i);             //重载[],实现数组运算
    const char& operator[](int i) const;       // 重载[],实现数组运算,对象为常量
    MyString& operator+=(const MyString& s);   //重载+=,实现与对象相加
    MyString& operator+=(const char* s);       //重载+=,实现与字符串相加
    friend ostream& operator>
    friend bool operator(const MyString& left, const MyString& right);     //重载>
    friend bool operator==(const MyString& left, const MyString& right);    //重载==
    friend bool operator!=(const MyString& left, const MyString& right);    //重载!=

    char* getData();
    int length();

private:
    int size;       //data表示的字符串长度
    char* data;     //指向字符串数据
};

   MyString.cpp

#include "MyString.h"

MyString::MyString()
{
    data = new char[1];
    *data = '\0';
    size = 0;
}

MyString::MyString(int n, char c)
{
    size = n;
    data = new char[n + 1];
    char* temp = data;
    while (n--)
    {
        *temp++ = c;
    }
    *temp = '\0';

}

MyString::MyString(const char* source)
{
    if (source == nullptr)
    {
        data = new char[1];
        *data = '\0';
        size = 0;
    }
    else
    {
        size = strlen(source);
        data = new char[size + 1];
        strcpy_s(data, size + 1, source);
    }
}

MyString::MyString(const MyString& s)
{
    size = s.size;
    data = new char[s.size + 1];
    strcpy_s(data, s.size + 1, s.data);
}

MyString& MyString::operator=(char* s)
{
    if (data != nullptr)
        delete[] data;
    size = strlen(s);
    data = new char[size + 1];
    strcpy_s(data, size + 1, s);
    return *this;
}

MyString& MyString::operator=(const MyString& s)
{
    if (this == &s)
        return *this;
    if (data != nullptr) //先释放内存
        delete[] data;
    size = s.size;
    data = new char[size + 1];
    strcpy_s(data, size + 1, s.data);
    return *this;
}

MyString::~MyString()
{
    if (data != nullptr)
    {
        delete[] data;
        data = nullptr;
        size = 0;
    }
}

char& MyString::operator[](int i)
{
    return data[i];
}

const char& MyString::operator[](int i) const
{
    return data[i];
}

MyString& MyString::operator+=(const MyString& s)
{
    int len = size + s.size + 1;
    char* temp = data;
    data = new char[len];
    strcpy_s(data, size + 1, temp);
    strcat_s(data, len, s.data);
    delete[] temp;
    size = len - 1;
    return *this;
}

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

    int len = size + strlen(s) + 1;
    char* temp = data;
    data = new char[len];
    strcpy_s(data, size + 1, temp);
    strcat_s(data, len, s);
    delete[] temp;
    size = len - 1;
    return *this;
}

char* MyString::getData()
{
    return data;
}

int MyString::length()
{
    return size;
}

ostream& operator            
关注
打赏
1652240117
查看更多评论
0.0370s