仿照string类,实现mystring类

#include <iostream>
#include <string.h>

using namespace std;

class mystring
{
private:
    char *str;       //记录c风格的字符串
    int size;        //记录字符串的实际长度

public:
    //无参构造
    mystring():size(10)
    {
        str = new char[size];  //构造一个长度为10的字符串
        strcpy(str,"");
    }
    //有参构造
    mystring(const char *s)
    {
        size = strlen(s);
        str = new char[size+1];
        strcpy(str,s);
        cout<<"mystring::有参构造 str = "<<str<<"    this = "<<this<<endl;
    }

    //拷贝构造
    mystring(const mystring &other):str(new char(*(other.str))),size(other.size)
    {
        strcpy(this->str,other.str);   //拷贝字符串
        this->size = other.size;       //拷贝字符串长度
        cout<<"mystring::拷贝构造 str = "<<str<<"    this = "<<this<<endl;
    }

    //拷贝赋值
    mystring & operator=(const mystring &other)
    {
        if(this != &other)
        {
            strcpy(this->str,other.str);
            this->size = other.size;
        }
        cout<<"mystring::拷贝赋值函数 str = "<<str<<"   this = "<<this<<endl;
        return *this;
    }

    //析构函数
    ~mystring()
    {
        delete str;
        cout<<"mystring::析构函数 this = "<<this<<endl;
    }

    //判空函数
    bool empty()
    {
        return 0==size;
    }

    //size函数
    int  mystring_size()
    {
        return strlen(str);
    }

    //at函数
    char &at(int pos)
    {
        return str[pos-1];
    }

    //c_str函数
    const char *c_str()
    {
        return this->str;
    }
};

int main()
{
    mystring m1("hello");

    mystring m2(m1);

    mystring m3;
    m3 = m2;

    cout<<m1.empty()<<endl;

    cout<<"字符串长度为:"<<m1.mystring_size()<<endl;

    cout<<"at(1) = "<<m1.at(1)<<endl;

    cout<<"c_str:"<<m1.c_str()<<endl;

    return 0;
}

运行结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你实现一个简单的mystring。 mystring的定义如下: ```c++ class mystring { public: // 构造函数 mystring(); mystring(const char* str); mystring(const mystring& other); // 析构函数 ~mystring(); // 成员函数 size_t length() const; const char* c_str() const; mystring& operator=(const mystring& other); mystring& operator+=(const mystring& other); char& operator[](size_t index); const char& operator[](size_t index) const; // 友元函数 friend bool operator==(const mystring& lhs, const mystring& rhs); friend bool operator!=(const mystring& lhs, const mystring& rhs); friend bool operator<(const mystring& lhs, const mystring& rhs); friend bool operator<=(const mystring& lhs, const mystring& rhs); friend bool operator>(const mystring& lhs, const mystring& rhs); friend bool operator>=(const mystring& lhs, const mystring& rhs); friend mystring operator+(const mystring& lhs, const mystring& rhs); private: char* m_data; // 字符串数据 size_t m_len; // 字符串长度 }; ``` 下面是成员函数的实现: ```c++ #include <cstring> #include <iostream> class mystring { public: // 构造函数 mystring() : m_data(nullptr), m_len(0) {} mystring(const char* str) : m_len(std::strlen(str)) { m_data = new char[m_len + 1]; std::strcpy(m_data, str); } mystring(const mystring& other) : m_len(other.m_len) { m_data = new char[m_len + 1]; std::strcpy(m_data, other.m_data); } // 析构函数 ~mystring() { delete[] m_data; } // 成员函数 size_t length() const { return m_len; } const char* c_str() const { return m_data; } mystring& operator=(const mystring& other) { if (this != &other) { delete[] m_data; m_len = other.m_len; m_data = new char[m_len + 1]; std::strcpy(m_data, other.m_data); } return *this; } mystring& operator+=(const mystring& other) { char* new_data = new char[m_len + other.m_len + 1]; std::strcpy(new_data, m_data); std::strcpy(new_data + m_len, other.m_data); delete[] m_data; m_data = new_data; m_len += other.m_len; return *this; } char& operator[](size_t index) { return m_data[index]; } const char& operator[](size_t index) const { return m_data[index]; } // 友元函数 friend bool operator==(const mystring& lhs, const mystring& rhs) { return std::strcmp(lhs.m_data, rhs.m_data) == 0; } friend bool operator!=(const mystring& lhs, const mystring& rhs) { return std::strcmp(lhs.m_data, rhs.m_data) != 0; } friend bool operator<(const mystring& lhs, const mystring& rhs) { return std::strcmp(lhs.m_data, rhs.m_data) < 0; } friend bool operator<=(const mystring& lhs, const mystring& rhs) { return std::strcmp(lhs.m_data, rhs.m_data) <= 0; } friend bool operator>(const mystring& lhs, const mystring& rhs) { return std::strcmp(lhs.m_data, rhs.m_data) > 0; } friend bool operator>=(const mystring& lhs, const mystring& rhs) { return std::strcmp(lhs.m_data, rhs.m_data) >= 0; } friend mystring operator+(const mystring& lhs, const mystring& rhs) { mystring result; result.m_len = lhs.m_len + rhs.m_len; result.m_data = new char[result.m_len + 1]; std::strcpy(result.m_data, lhs.m_data); std::strcpy(result.m_data + lhs.m_len, rhs.m_data); return result; } private: char* m_data; // 字符串数据 size_t m_len; // 字符串长度 }; ``` 上面的实现中,我们使用了C++标准库中的函数`std::strlen`、`std::strcpy`和`std::strcmp`。这些函数分别用于计算字符串长度、复制字符串和比较字符串。由于这些函数都是C语言标准库中的函数,因此它们在C++中也是可用的。 你可以使用下面的代码测试一下mystring的功能: ```c++ #include <iostream> #include "mystring.h" int main() { mystring s1 = "hello"; mystring s2 = "world"; std::cout << "s1: " << s1.c_str() << std::endl; std::cout << "s2: " << s2.c_str() << std::endl; std::cout << "s1 length: " << s1.length() << std::endl; std::cout << "s2 length: " << s2.length() << std::endl; std::cout << "s1 + s2: " << (s1 + s2).c_str() << std::endl; std::cout << "s1 == s2: " << (s1 == s2) << std::endl; std::cout << "s1 != s2: " << (s1 != s2) << std::endl; std::cout << "s1 < s2: " << (s1 < s2) << std::endl; std::cout << "s1 <= s2: " << (s1 <= s2) << std::endl; std::cout << "s1 > s2: " << (s1 > s2) << std::endl; std::cout << "s1 >= s2: " << (s1 >= s2) << std::endl; s1 += s2; std::cout << "s1 += s2: " << s1.c_str() << std::endl; return 0; } ``` 输出结果如下: ``` s1: hello s2: world s1 length: 5 s2 length: 5 s1 + s2: helloworld s1 == s2: 0 s1 != s2: 1 s1 < s2: 1 s1 <= s2: 1 s1 > s2: 0 s1 >= s2: 0 s1 += s2: helloworld ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值