从造轮子做起:String

实现简单的 String 类,采用深拷贝的形式。

#include <iostream>
#include <string.h>
#include <assert.h>
using namespace std;

class String {
public:
    String(const char* str = " ") {
        size_t len = strlen(str);
        _str = new char[len + 1];
        strcpy(_str, str);
        _size = len;
        _capacity = len;
    }

    void Reserve(size_t n) {
        Expand(n);
    }

    void Resize(size_t n, const char ch = '\0') {
        if (n < _capacity) {
            _size = n;
            _str[_size] = '\0';
        } else {
            Expand(n);
            for (size_t i = _size; i < n; ++i) {
                _str[i] = ch;
            }
            _str[n] = '\0';
            _size = n;
        }
    }

    void Swap(String& s) {
        swap(_str, s._str);
        swap(_size, s._size);
        swap(_capacity, s._capacity);
    }

    String(const String& s) 
        :_str(NULL)
    {
        String tmp(s._str);
        tmp.Swap(*this);
    }

    String& operator=(String s) {
        if (_str == s._str)
            return *this;

        this->Swap(s);
        return *this;
    }

    ~String() {
        if (_str == NULL)
            return;
        delete[] _str;
        _str = NULL;
        _size = 0;
        _capacity = 0;
    }

    void Expand(size_t n) {
        if (n < _capacity)
            return;

        char* tmp = new char[n + 1];
        strcpy(tmp, _str);
        delete[] _str;
        _str = tmp;
        _capacity = n;
    }

    void Append(const char* str) {
        if (str == NULL)
            return;

        size_t len = strlen(str);
        if (_capacity < _size + len)
            Expand(_size + len);
        strcpy(_str + _size, str);
    }

    String& operator+=(const char* str) {
        Append(str);
        return *this;
    }

    String& operator+=(const String& s) {
        Append(s._str);
        return *this;
    }

    String operator+(const char* str) {
        String ret(*this);
        ret += str;
        return ret;
    }

    String operator+(const String& s) {
        String ret(*this);
        ret += s;
        return ret;
    }

    char& operator[](size_t pos) {
        assert(pos < _size);
        return _str[pos];
    }

    size_t Size() {
        return _size;
    }

    size_t Capacity() {
        return _capacity;
    }

    void Insert(size_t pos, char val) {
        assert(pos <= _size);

        if (_size >= _capacity)
            Expand(_capacity * 2 + 1);
        if (pos != _size) {
            for (size_t i = _size; i > pos; --i) {
                _str[i] = _str[i - 1];
            }
        }
        _str[pos] = val;
        ++_size;
        _str[_size] = '\0';
    }

    void Erase(size_t pos) {
        assert(pos < _size);

        if (pos != _size - 1) {
            for (size_t i = pos; i + 1 < _size; ++i) {
                _str[i] = _str[i + 1];
            }
        }
        --_size;
        _str[_size] = '\0';
    }

    bool operator>(const String& s) {
        int ret = strcmp(_str, s._str);
        if (ret > 0)
            return true;
        else
            return false;
    }

    bool operator==(const String& s) {
        int ret = strcmp(_str, s._str);
        if (ret == 0)
            return true;
        else
            return false;
    }

    bool operator<(const String& s) {
        return !(*this == s || *this > s);
    }

    bool operator>=(const String& s) {
        return !(*this < s);
    }

    bool operator<=(const String& s) {
        return !(*this > s);
    }

    bool operator!=(const String& s) {
        return !(*this == s);
    }

    char* C_Str() {
        return _str;
    }

private:
    char* _str;
    size_t _size;
    size_t _capacity;
};



欢迎大家共同讨论,如有错误及时联系作者指出,并改正。谢谢大家!

猜你喜欢

转载自blog.csdn.net/liuchenxia8/article/details/81708017