zoukankan      html  css  js  c++  java
  • C++ 实现简单的string

    /*
    实现一个string满足基本用法
    */
    
    class MyString
    {
    public:
        //默认参数
        MyString(const char *str=""):m_str(strcpy(new char[strlen(str)+1], str))
        {
    
        }
    
        ~MyString(void)
        {
            if (m_str)
            {//数组形式的删除内存
                delete []m_str;
                m_str = nullptr;
            }
        }
    
        //深拷贝构造
        MyString(const MyString&that):m_str(strcpy(new char[strlen(that.m_str) + 1], that.m_str))
        {
    
        }
        
        //深拷贝赋值
        MyString&operator=(const MyString&that)
        {
            //防止紫赋值
            if (&that!=this)
            {
                MyString temp(that);//深拷贝构造,temp是i2的临时对象
                swap(m_str, temp.m_str);
            }
    
            return  *this;
        }
    
        
        const char *c_str(void)const
        {
            return m_str;
        }
    
    private:
        char* m_str;
    };
  • 相关阅读:
    Codeforces Global Round 6
    Codeforces Global Round 5
    笔记
    笔记
    Codeforces Round #608 (Div. 2)
    模板
    Codeforces Round #607 (Div. 2)
    find命令
    while循环脚本
    发邮件
  • 原文地址:https://www.cnblogs.com/LuckCoder/p/14549998.html
Copyright © 2011-2022 走看看