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;
}
相关推荐
Georgewu7 小时前
【AI大模型入门指南】提示词Prompt工程详解
算法·aigc·ai编程
ZackSock13 小时前
Policy Gradient 极简教程
算法
Big_Yellow_J14 小时前
深入浅出了解生成模型-3:Diffusion模型原理以及代码
算法·面试
ZackSock15 小时前
从零实现 RAG
算法
Jolyne_15 小时前
前端常用的树处理方法总结
前端·算法·面试
前端付豪17 小时前
微信视频号推荐系统揭秘:兴趣建模、多模态分析与亿级流控架构实战
前端·后端·算法
木杉苑17 小时前
快速幂算法
算法
-qOVOp-20 小时前
408第一季 - 数据结构 - 排序II
数据结构·算法·排序算法
小胖同学~20 小时前
快速入门数据结构--栈
算法
C++ 老炮儿的技术栈20 小时前
VSCode -配置为中文界面
大数据·c语言·c++·ide·vscode·算法·编辑器