c++day4

仿照string类,完成myString 类

cpp 复制代码
#include <iostream>
#include<cstring>

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)          //string  s("hello world")
        {
            size = strlen(s);
             str = new char[size+1];
             strcpy(str, s);
        }
        //拷贝构造
        myString(const myString &other):str(new char(*(other.str))),size(other.size)
        {
            strcpy(this->str,other.str);
            this->size=other.size;
            cout<<"拷贝构造函数"<<endl;
        }
        //析构函数
        ~myString()
        {
            delete str;
            cout<<"析构函数:"<<this<<endl;

        }
        //拷贝赋值函数
        myString & operator=(const myString &other)
        {
            if(this != &other)          //确定不是自己给自己赋值
            {
                this->size = other.size;

                //判断原来指针空间释放被清空
                if(this->str != NULL)
                {
                    delete this->str;
                }
                this->str = new char(*other.str);

            }
            cout<<"拷贝赋值函数"<<endl;
            return  *this;             //返回自身引用
        }
        //判空函数
        bool empty()
        {
            return 0==size;
        }
        //size函数
        int mystring_size()
        {
            return strlen(str);
        }
        //c_str函数
        char *c_str()
        {
            return this->str;
        }
        //at函数
        char &at(int pos)
        {
            return str[pos-1];
        }
        //加号运算符重载
        const myString operator+(const myString &R)
        {
            myString c;
                // 计算合并后的字符串长度
            c.size=this->size+R.size;
            // 复制第一个字符串到结果字符串
            memcpy(c.str,this->str,this->size);
            // 复制第二个字符串到结果字符串
            memcpy(c.str+this->size,R.str,R.size+1);
            return c;
        }
        //加等于运算符重载
        myString & operator+=(const myString &R)
        {
            // 复制第二个字符串到第一个字符串后
            memcpy(this->str+this->size,R.str,R.size+1);
            // 计算合并后的字符串长度
            this->size=this->size+R.size;
            return *this;
        }
        //关系运算符重载(>)
        bool operator>(const myString &R)const
        {
            return strcmp(this->str,R.str)>0;
        }
        //中括号运算符重载
        char & operator[](int index)
        {
            return this->str[index];
        }
};

int main()
{

    myString s1("hello");
    //判空函数
    if(s1.empty())
    {
        cout<<"函数为空"<<endl;
    }
    else
    {
        cout<<"函数不为空"<<endl;
    }
    //打印s1的长度
    cout<<"size="<<s1.mystring_size()<<endl;
    //打印s1字符串内容
    cout<<"s1="<<s1.c_str()<<endl;
    //调用&at函数,打印s1[1]的内容
    cout<<"s1[1]="<<s1.at(2)<<endl;
    myString s2("world");
    //打印s2字符串内容
    cout<<"s2="<<s2.c_str()<<endl;
    //调用+=运算符重载函数
    s2+=s1;
    //打印更新之后s2的字符串内容
    cout<<"s2="<<s2.c_str()<<endl;
    //调用+号运算符重载
    myString s3=s1+s2;
    //打印s3字符串内容
    cout<<"s3="<<s3.c_str()<<endl;
    //调用[]运算符重载
    cout<<"s3[5]="<<s3[6]<<endl;
    return 0;
}
相关推荐
2601_967760782 分钟前
2026年PDF压缩与页码添加工具技术实测:性能、算法与本地化适配深度对比
算法·pdf
不会就选b1 小时前
算法日常・每日刷题--<贪心>7
数据结构·算法·leetcode
moonrailgun1 小时前
用 Node.js 复刻 Codex Astra 的终端星光
前端·javascript·算法
罗西的思考2 小时前
[Agent Memory / 强化学习] MemPO源码学习笔记 ---(1)--- 总体
人工智能·算法·机器学习
lvwangshu2 小时前
图论:LCA、树的直径、树的重心、二分图与 Tarjan 缩点
算法·图论
302wanger3 小时前
干与湿:AI 拿走脑力之后,人剩下什么
算法
计算机编程-吉哥4 小时前
脑肿瘤MRI智能识别系统:基于深度学习的像素级脑肿瘤语义分割平台【计算机毕业设计选题推荐】
人工智能·python·深度学习·算法·毕业设计·课程设计·大数据毕业设计选题推荐
用户204937554954 小时前
端侧语音部署踩坑:模型能跑不等于终端真的能用
后端·算法
shehuiyuelaiyuehao5 小时前
算法39,位运算,消失的两个数字
java·数据结构·算法
ZiLing5 小时前
2026 ROS 2 Lyrical 踩坑实录(一):编译与依赖——rosdep、现代 CMake 与 CMake 4.x
算法