c++----Simulate the implementation of the String class

The traditional way of writing is to go step by step, the
modern way of writing is to reuse, call other functions to achieve

class String
{
public:
    //构造函数
    //String()
    //  :_str(new char[1])
    //{
    //  _str[0] = '\0';
    //}
    String(const char* str="")
        :_str(new char[strlen(str) + 1])
    {
        strcpy(_str, str);
    }
    //传统写法
    //String(const String& s)//拷贝构造,s3(s2)
    //  :_str(new char[strlen(s._str)+1])
    //{
    //  strcpy(_str,s._str);
    //}
    //String & operator=(const String& s)//s1=s2
    //{
    //  if (&s != this)
    //  {
    //      char* str = new char[strlen(s._str) + 1];
    //      delete[] _str;
    //      _str = str;
    //      strcpy(_str, s._str);
    //  }
    //  return *this;
    //}
    //现代写法
    String(const String& s)//拷贝构造,String s3(s2)
        :_str(NULL)
    {
        String tmp(s._str);
        swap(_str,tmp._str);
    }
    String & operator=(const String& s)//s1=s2,这块自己给自己赋值也是可以的,析构在最后,所以不会出错
    {
        if (&s != this)
        {
            String tmp(s._str);
            swap(tmp._str, _str);
        }
        return *this;
    }
    const char* c_str()//返回c形式的字符串
    {
        return _str;
    }
    ~String()//析构函数
    {
        if (_str)
            delete[] _str;
    }
private:
    char* _str;
};
void TestString()
{
    String s1;
    String s2("change world");
    cout << s2.c_str() << endl;
}

Mock the implementation of the string class

//现代写法的本质-复用
class String
{
public:
    String(const char* str = "")//构造函数
    {
        _size = _capacity = strlen(str);
        _str = new char[_capacity + 1];
        strcpy(_str, str);
    }
    //s1.Swap(s2)
    void Swap(String& s)
    {
        swap(_str, s._str);
        swap(_size, s._size);
        swap(_capacity, s._capacity);
    }
    String(const String& s)//拷贝构造,String s3(s2)
        :_str(NULL)
        , _size(0)
        , _capacity(0)
    {
        String tmp(s._str);
        Swap(tmp);
    }
    //s1=s2
    String & operator=(String s)//s1=s2,这块自己给自己赋值也是可以的,析构在最后,所以不会出错
    {
        Swap(s);
        return *this;
    }
    const char* c_str()const//返回c形式的字符串
    {
        return _str;
    }
    ~String()//析构函数
    {
        if (_str)
        {
            delete[] _str;
            _size = _capacity = 0;
            _str = NULL;
        }
    }
    void Expand(size_t n)//扩容
    {
        if (n > _capacity)
        {
            char* newstr = new char[n + 1];
            strcpy(newstr, _str);
            delete[] _str;
            _str = newstr;
            _capacity = n;
        }
    }
    void PushBack(char ch)
    {
        if (_size >= _capacity)
        {
            Expand(_capacity * 2);
        }
        _str[_size++] = ch;
        _str[_size] = '\0';
    }
    void PushBack(const char* str)
    {
        size_t len = strlen(str);
        if (_size + len > _capacity)
        {
            Expand(_size + len);
        }
        strcpy(_str + _size, str);
        _size += len;
    }
    void PopBack()
    {
        _str[_size] = '\0';
        _size--;
    }
    void PushFront(char ch)
    {
        Insert(0, ch);
    }
    void PushFront(const char* str)
    {
        Insert(0, str);
    }
    //void PopFront();
    void Insert(size_t pos, char ch)
    {
        assert(pos <= _size);
        if (_size >= _capacity)
        {
            Expand(_capacity * 2);
        }
        for (int i = _size; i >= pos; --i)
        {
            _str[i + 1] = _str[i];
        }
        _str[pos] = ch;
        _size++;
    }
    void Insert(size_t pos, const char* str)
    {
        assert(pos <= _size);
        const char* cur = str;
        size_t len = strlen(str);
        if (len + _size > _capacity)
            Expand(len + _size);
        for (int i = _size; i >= (int)pos; --i)
        {
            _str[i + len] = _str[i];
        }
        for (int i = 0; i < len; i++)
            _str[pos++] = str[i];
        _size += len;
    }
    void Erase(size_t pos, size_t n = 1)
    {
        if (n == 0)
        {
            return;
        }
        else if (pos + n >= _size)
        {
            _str[pos] = '\0';
            _size = pos;
        }
        else
        {
            strcpy(_str + pos, _str + pos + n);
            _size -= n;
        }
    }
    void Replace(char ch1, char ch2)//替换字符串中某个字符
    {
        char* str = _str;
        while (*str != '\0')
        {
            if (*str == ch1)
                *str = ch2;
            str++;
        }
    }
    size_t Find(char ch)
    {
        char* str = _str;
        while (*str != '\0')
        {
            if (*str == ch)
                return (int)(str - _str);
            str++;
        }
        return -1;
    }
    size_t Find(const char* str)
    {
        const char* str1 = _str;
        const char* str2 = str;
        while (*str1)
        {
            const char* cur = str1;
            const char* sub = str2;
            while (*str1 == *sub && sub!='\0')
            {
                ++cur;
                ++sub;
            }
            if (*sub == '\0')
            {
                return str1 - str;
            }
            else
            {
                sub = str;
                str1++;
            }
        }
        return -1;
    }
    //s1+'a'
    String operator+(char ch)
    {
        String tmp(*this);
        tmp.PushBack(ch);
        return tmp;
    }
    String& operator+=(char ch)
    {
        PushBack(ch);
        return *this;
    }
    String operator+(char* str)
    {
        String tmp(*this);
        tmp.PushBack(str);
        return tmp;
    }
    String& operator+=(char* str)
    {
        PushBack(str);
        return *this;
    }
    bool operator>(const String& s)const
    {
        const char* str1 = _str;
        const char* str2 = s._str;
        while (*str1&&*str2)
        {
            if (*str1 > *str2)
                return true;
            else if (*str1 < *str2)
                return false;
            else
            {
                ++str1;
                ++str2;
            }
        }
        if (*str1)
            return true;
        else
            return false;
    }
    bool operator>=(const String& s)const
    {
        return *this > s || *this == s;
    }
    bool operator<(const String& s)const
    {
        return !(*this >= s);
    }
    bool operator<=(const String& s)const
    {
        return !(*this>s);
    }
    bool operator==(const String& s)const
    {
        const char* str1 = _str;
        const char* str2 = s._str;
        while (*str1&&*str2)
        {
            if (*str1 != *str2)
                return false;
            else
            {
                ++str1;
                ++str2;
            }
        }
        if (*str1 == '\0'&&*str2 == '\0')
        {
            return false;
        }
        else
            return true;
    }
    bool operator!=(const String& s)const
    {
        return !(*this == s);
    }
    char& operator[](size_t pos)
    {
        return _str[pos];
    }
    const char& operator[](size_t pos)const
    {
        return _str[pos];
    }
private:
    char* _str;
    size_t _size;
    size_t _capacity;
};

If you have any questions, you can comment and tell me, I hope to guide!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324840278&siteId=291194637