c++ MyString类实现

string类中各成员函数的实现

一、string类的定义

class MyString
{
public:
    MyString();
    MyString(char *sz);
    MyString(const MyString &other);
    MyString& operator=(const MyString &rhs);
    MyString& operator=(const char *str);
    char& operator[](unsigned int index);
    const char& operator[](unsigned int index) const;
    friend MyString operator+(const MyString &s1, const MyString &s2);
    friend MyString& operator+=(MyString &str,const MyString &str2);
    friend ostream& operator<<(ostream &os, const MyString &str);
    friend istream& operator>>(istream &is, MyString &str);
    bool isEmpty();

    ~MyString();

    int assign(const char *obj);
    void display() const;
    int replace(const char *from, const char *to);
    int append(const char *obj);
    unsigned int getSize() const;

private:
    char *m_data;
    unsigned int m_len;
};

二、类成员函数的实现

MyString::MyString()
{
    m_data = NULL;
    m_len = 0;
}

MyString::MyString(char *sz)
{
    m_len = strlen(sz);
    m_data = new char[m_len + 1];
    strcpy(m_data, sz);
}

MyString::MyString(const MyString &other)
{
    m_len = other.m_len;
    m_data = new char[m_len];
    strcpy(m_data, other.m_data);
}

MyString& MyString::operator=(const MyString &rhs)
{
    if(this == &rhs)
    {
        return *this;
    }

    if(m_data != NULL)
    {
        delete []m_data;
    }
    m_len = rhs.m_len;
    m_data = new char[m_len + 1];
    strcpy(m_data, rhs.m_data);

    return *this;
}

MyString& MyString::operator=(const char *str)
{
    if(m_data != NULL)
    {
        delete []m_data;
    }
    m_len = strlen(str);
    m_data = new char[m_len + 1];
    strcpy(m_data, str);

    return *this;
}

char& MyString::operator[](unsigned int index)
{
    cout << "char& MyString::operator[](unsigned int index)" << endl;
    if(index > m_len)
    {
        return 0;
    }
    return *(m_data + index); //m_data[index]
}

const char& MyString::operator[](unsigned int index) const
{
    cout << "const char& MyString::operator[](unsigned int index) const" << endl;
    if(index > m_len)
    {
        return 0;
    }
    return *(m_data + index);
}
bool MyString::isEmpty()
{
   return m_len == 0; //m_len
}

MyString::~MyString()
{
    if(m_data != NULL)
    {
        delete []m_data;
    }
}

int MyString::assign(const char *obj)
{
    if(m_data != NULL)
    {
        delete []m_data;
    }

    m_len = strlen(obj);
    m_data = new char[m_len + 1];
    strcpy(m_data, obj);
}

void MyString::display() const
{
    if(m_data == NULL)
    {
        cout << "str is NULL" <<endl;
    }

    cout << m_data << endl;
}

int MyString::replace(const char *from, const char *to)
{
    char *q,*tmp,*cur;
    tmp = strstr(m_data, from);
    if(strlen(from) < strlen(to) || tmp == NULL)
    {
        return -1;
    }
    char *p = new char[m_len];
    q = p;
    cur = m_data;
    
    //定位到子串起始处
    for(int i = 0; i < (tmp - m_data); i++)
    {
        *q = *cur;
        q++;
        cur++;
    }
    *q = '\0';
    strcat(p, to);

    tmp = tmp + strlen(from);
    strcat(p,tmp); //拼接剩余部分

    m_data = p;

    return 0;
}

int MyString::append(const char *obj)
{
    m_len += strlen(obj);
    char *tmp = new char[m_len + 1];
    char *p = tmp, *q = m_data;
    while(*q != '\0')
    {
        *p = *q;
        p++;
        q++;
    }
    if(m_data != NULL)
    {
        delete []m_data;
        m_data = NULL;
    }
    strcat(tmp,obj);

    m_data = tmp;
    return 0;
}

unsigned int MyString::getSize() const
{
    return m_len;
}

bool MyString::operator==(MyString &another)
{
    if(this == &another)  //不能是其本身
    {
        return false;
    }
    if(this->m_len != another.m_len)
    {
        return false;
    }
    else if(strcmp(this->m_data, another.m_data) != 0)
    {
        return false;
    }

    return true;
}

bool MyString::operator!=(MyString& another)
{
    return !(*this == another);
}

三、友元函数的实现

MyString operator+(const MyString &s1, const MyString &s2)
{
    int len = strlen(s1.m_data) + strlen(s2.m_data);
    MyString tmp;
    tmp.m_data = new char[len + 1];
    tmp.m_len = len;
    strcat(tmp.m_data,s1.m_data);
    strcat(tmp.m_data,s2.m_data);

    return tmp;
}

MyString& operator+=(MyString &str,const MyString &str2)
{
    int len = strlen(str.m_data) + strlen(str2.m_data);
    char *tmp = new char[len + 1];
    strcpy(tmp,str.m_data);
    strcat(tmp,str2.m_data);
    if(str.m_data != NULL)
    {
        delete []str.m_data;
    }
    str.m_len = len;
    str.m_data = tmp;

    return str;
}

ostream& operator<<(ostream &os, const MyString &str)
{
    os << str.m_data;

    return os;
}

istream& operator>>(istream &is, MyString &str)
{
    if(str.m_data != NULL)
    {
        delete []str.m_data;
        str.m_data = NULL;
        str.m_len = 0;
    }

    char tmp[1024] = {0};
    cin >> tmp;
    str.m_len = strlen(tmp);
    str.m_data = new char[str.m_len + 1];

    strcpy(str.m_data,tmp);

    return is;
}

注:

1、strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回 str1字符串从 str2第一次出现的位置开始到 str1结尾的字符串;否则,返回NULL。

2、isspace()函数:用于测定字符串是否为空字符串<ctype.h>

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是一个简单的mystring的设计。 ```c++ class mystring { private: int length; // 字符串长度 char* str; // 存储字符串的字符数组 public: mystring(); // 默认构造函数 mystring(const char* s); // 带参构造函数 mystring(const mystring& s); // 拷贝构造函数 ~mystring(); // 析构函数 int size() const; // 返回字符串长度 bool empty() const; // 判断字符串是否为空 void clear(); // 清空字符串 const char* c_str() const; // 返回C风格字符串 mystring& operator=(const mystring& s); // 赋值运算符重载 mystring& operator+=(const mystring& s); // 加等运算符重载 char& operator[](int index); // 下标运算符重载 const char& operator[](int index) const; // 下标运算符重载(常量对象) bool operator==(const mystring& s) const; // 相等运算符重载 bool operator!=(const mystring& s) const; // 不等运算符重载 bool operator<(const mystring& s) const; // 小于运算符重载 bool operator>(const mystring& s) const; // 大于运算符重载 bool operator<=(const mystring& s) const; // 小于等于运算符重载 bool operator>=(const mystring& s) const; // 大于等于运算符重载 friend std::ostream& operator<<(std::ostream& os, const mystring& s); // 输出运算符重载 friend std::istream& operator>>(std::istream& is, mystring& s); // 输入运算符重载 }; ``` 下面是这些成员函数的具体实现。 ```c++ mystring::mystring() { length = 0; str = new char[1]; str[0] = '\0'; } mystring::mystring(const char* s) { length = strlen(s); str = new char[length + 1]; strcpy(str, s); } mystring::mystring(const mystring& s) { length = s.length; str = new char[length + 1]; strcpy(str, s.str); } mystring::~mystring() { delete[] str; } int mystring::size() const { return length; } bool mystring::empty() const { return length == 0; } void mystring::clear() { delete[] str; length = 0; str = new char[1]; str[0] = '\0'; } const char* mystring::c_str() const { return str; } mystring& mystring::operator=(const mystring& s) { if (this != &s) { delete[] str; length = s.length; str = new char[length + 1]; strcpy(str, s.str); } return *this; } mystring& mystring::operator+=(const mystring& s) { char* temp = new char[length + s.length + 1]; strcpy(temp, str); strcat(temp, s.str); delete[] str; str = temp; length += s.length; return *this; } char& mystring::operator[](int index) { return str[index]; } const char& mystring::operator[](int index) const { return str[index]; } bool mystring::operator==(const mystring& s) const { return strcmp(str, s.str) == 0; } bool mystring::operator!=(const mystring& s) const { return strcmp(str, s.str) != 0; } bool mystring::operator<(const mystring& s) const { return strcmp(str, s.str) < 0; } bool mystring::operator>(const mystring& s) const { return strcmp(str, s.str) > 0; } bool mystring::operator<=(const mystring& s) const { return strcmp(str, s.str) <= 0; } bool mystring::operator>=(const mystring& s) const { return strcmp(str, s.str) >= 0; } std::ostream& operator<<(std::ostream& os, const mystring& s) { os << s.str; return os; } std::istream& operator>>(std::istream& is, mystring& s) { char temp[1024]; is >> temp; s = mystring(temp); return is; } ``` 这里只是提供了一个简单的mystring的设计,如果需要更加完善的功能,可以根据实际需求进行扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Star星屹程序设计

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值