仿照string类,实现myString类

#include <iostream>

#include <string.h>

#include<string>
using namespace std;



class myString
{
private:
    char *str;
    int  size;
public:
    myString():size(10)
    {
        str = new char [size];
        strcpy(str,"");
        cout << this->str<<endl;
    }

    myString(const char *str)
    {
        size = strlen(str);
        this->str = new char [size+1];
        strcpy(this->str , str);
        cout << this->str<<endl;
    }

    myString(const myString &other):str(new char[other.size+1]),size(other.size)
    {

        int i = 0 ;

        while (*(other.str+i) != '\0' )
        {
            this->str[i] = *(other.str+i);
            i++;
        }
        cout << this->str<<endl;
    }
    ~myString()
    {
        delete []this->str;
        cout << "析构成功" << endl;

    }

    void judge_empty()
    {
        if(!*(this->str))
        {
            cout << "空" <<endl;
        }
        else
         {
             cout << "非空" <<endl;
        }
    }

    int len_size ()
    {
        return this->size;
    }

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

   char at (int i)
   {
       return  *(str+i);
   }
};

int main()
{
    myString s1("hello");
    myString s2(s1);
    cout <<"s1 size:"<<s1.len_size()<<endl;
    cout<<"第1个字符为"<<s1.at(0)<<endl;
    cout<<"第2个字符为"<<s1.at(1)<<endl;
    cout<<"第3个字符为"<<s1.at(2)<<endl;
    cout << "s1";
    s1.judge_empty();

    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、付费专栏及课程。

余额充值