模拟实现String的增删查改

本篇模拟实现String的增删查改功能,采用现代写法。

具体实现如下:

class String
{
public:
   //构造函数
    String(const char* str)
        :_str(new char[strlen(str)+1])
    {
        _size = strlen(str);
        _capacity = _size;
        strcpy(_str, str);

    }

    //实现复用,将其写为函数
    void Swap(String& s)
    {
        swap(_str, s._str);
        swap(_size, s._size);
        swap(_capacity, s._capacity);

    }
    //拷贝构造函数s2(s1)
    String(const String& s)
        :_str(NULL)
        , _size(0)
        , _capacity(0)
    {
        String tmp(s._str);//采用现代写法
        Swap(tmp);
    }

    //s2=s1
    String& operator=( String s)
    {
        if (this != &s)
        {
            Swap(s);
        }
        return *this;
    }

     //析构函数
    ~String()
    {
        if (_str)
        {
            delete[] _str;
            _str = NULL;
        }
    }



    //增容
    void Expand(size_t n)
    {
        char *newstr = new char[n + 1];//开空间
        strcpy(newstr, _str);
        delete[] _str;
        _str = newstr;
        _capacity = n;

    }

    //尾插字符
    void PushBack(char c)
    {
        if (_size >= _capacity)//扩容
        {
            Expand(_capacity * 2);
        }
        _str[_size++] = c;
        _str[_size] = '\0';
    }

    //尾插字符串
    void PushBack(const char *str)
    {
        size_t len = strlen(str);
        if (_size+len > _capacity)//扩容
        {
            Expand(len + _size);

        }
        strcpy(_size+_str, str);//拷贝字符串
        _size += len;

    }

    //头插字符
    void PushFront(char c)
    {
        if (_size>=_capacity)//扩容
        {
            Expand(_capacity * 2);

        }
        //后移字符串中的字符
        for (int i = _size; i >= 0; --i)
        {
            _str[i + 1] = _str[i];
        }
        _str[0] = c;
        _size++;
    }

    //头插字符串
    void PushFront(const char *str)
    {
        size_t len = strlen(str);
        if (_size + len > _capacity)//扩容
        {
            Expand(_size + len);
        }
        //后移字符串
        for (int i = _size; i >= 0; --i)
        {
            _str[len + i] = _str[i];
        }
        //字符串拷贝
        char *p = _str;
        while (*str != '\0')
        {
            *p++ = *str++;
        }
        _size += len;
    }

    //任意位置插入字符
    void Insert(size_t pos, char c)
    {
        assert(pos <= _size);
        if (_size > _capacity)
        {
            Expand(_capacity * 2);
        }
        //移数据
        for (int i = _size; i >= (int)pos; --i)
        {
            _str[i + 1] = _str[i];
        }
        _str[pos] = c;
        _size++;
    }

     //任意位置插入字符串
    void Insert(size_t pos, char *str)
    {
        size_t len = strlen(str);
        if (_size + len > _capacity)//扩容
        {
            Expand(_size + len);
        }
        //后移数据
        for (int i = _size; i >= (int)pos; --i)
        {
            _str[len + i] = _str[i];
        }
        //拷贝字符串
        while (*str != '\0')
        {
            _str[pos++] = *str++;
        }
        _size += len;
    }

      //删除长度为n的字符串
    void Erase(size_t pos, size_t len)
    {
        assert(pos < _size);
        if (pos + len>=_size)//pos位置之后全为0
        {
            _str[pos] = '\0';
            _size = pos;
        }
        else
        {
            strcpy(_str + pos, _str + pos + len);
            _size -= len;
        }
    }

    String operator+(char c)
    {
        String tmp(*this);
        tmp.PushBack(c);
        return tmp;
    }

    String& operator+=(char c)
    {
        PushBack(c);
        return *this;
    }


    String operator+(const char *str)
    {
        String tmp(*this);
        tmp.PushBack(str);
        return tmp;
    }
    String& operator+=(const char *str)
    {
        PushBack(str);
        return *this;
    }
    //s1>s2
    bool operator>(const String& s) const
    {
        const char* p1 = _str;
        const char* p2 = s._str;
        while (*p1&&*p2)
        {
            if (*p1 > *p2)
            {
                return true;
            }
            else if (*p1 > *p2)
            {
                return false;
            }
            else
            {
                ++p1;
                ++p2;
            }

        }
        if (*p1)
            return true;
        if (*p2)
            return false;
    }

    bool operator==(const String& s) const
    {
        const char *p1 = _str;
        const char *p2 = s._str;
        while (*p1&&*p2)
        {
            if (*p1 != *p2)
            {
                return false;
            }
            else
            {
                ++p1;
                ++p2;
            }
        }
        if (*p1 != '\0'&&*p2 != '\0')
        {
            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);
    }

    //查找字符
    size_t Find(char c, size_t pos)
    {
        for (size_t i = pos; i < _size; ++i)
        {
            if (_str[i] == c)
                return i;//找到,返回下标
        }
        return -1;//未找到
    }

    //查找字符串
    size_t Find(const char* sub, size_t pos)
    {
        assert(sub);
        assert(pos <_size);

        const char* src = _str + pos;
        while (*src)
        {
            const char* match = sub;//如果不匹配,返回子串起始处重新查找
            const char *cur = src;
            while (*match&&*match == *cur)//结束条件
            {
                ++match;
                ++cur;
            }
            if (*match == '\0')//找到子串
            {
                return src - _str;//返回下标
            }
            else
            {
                ++src;
            }
        }
        return -1;//未找到
    }

    const char *c_str()
    {
        return _str;
     }

    //获取指定位置的字符
    char& operator[](size_t pos)
    {
        assert(pos < _size);
        return _str[pos];
    }
    const char& operator[](size_t pos)const
    {
        assert(pos < _size);
        return _str[pos];
    }
private:
    char *_str;
    size_t _size;
    size_t _capacity;
};
//测试部分

void Test()
{
    String s1("hello world");
    //s1.PushFront('J');
    //cout << s1.c_str() << endl;
    //String s2 = s1 + "123";
    //s2.PushFront("!!!!");
    //cout << s2.c_str()<< endl;

    s1 += 'x';
    //s1 +="wc";
    cout << s1.c_str() << endl;
    cout << s1.Find('w', 2) << endl;

    s1.Erase(1, 3);
    cout << s1.c_str() << endl;

    s1[0] = 'Q';
    cout << s1.c_str() << endl;

    String s3("change");
    const String s4(s3);
    cout << s4[3] << endl;
}

结果如下:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/kai29/article/details/80114376