模拟c++的string类

#include<iostream>
#include<assert.h>
using namespace std;
#pragma warning(disable:4996)
#define DEFAULT_CAPACITY 5

class String
{
public:
    String(const char* str = "")
        :_str(new char[strlen(str) + 1])
        , _size(strlen(str))
        , _capacity(DEFAULT_CAPACITY)
    {
        strcpy(_str,str);
    }
    String(const String& s)
        :_str(new char[strlen(s._str) + 1])
        , _size(s._size)
        , _capacity(s._capacity)
    {
        strcpy(_str, s._str);
    }
    ~String()
    {
        if (_str)
        {
            delete[] _str;
        }
    }
    String& operator=( String& s)
    {
        if (this != &s)
        {
            swap(_str, s._str);
            _size = s._size;
            _capacity = s._capacity;
        }
        return *this;
    }
public:
    void PushBack(char ch)
    {
        //_size = strlen(_str);
        CheekCapacity(_size + 2);
        _str[_size++] = ch;
        _str[_size] = '\0';
    }

    void PopBack()
    {
        if (_str != NULL)
        {
            //_size = strlen(_str);
            _str[_size - 1] = '\0';
            _size--;
        }
        else
        {
            cout << "string is empty" << endl;
            return;
        }
    }

    void Insert(int pos, char ch)
    {
        //_size = strlen(_str);
        CheekCapacity(_size + 2);
        if ((pos >= 0) && (pos <= _size))
        {
            for (int i = _size + 1; i > pos; i--)  //连‘\0’一起往后挪
            {
                _str[i] = _str[i - 1];
            }
            _str[pos] = ch;
            _size++;
        }
    }

    void InsertStr(int pos, const char* str)
    {
        int len = strlen(str);
        CheekCapacity(_size + 1 + len);
        if ((pos >= 0) && (pos <= _size))
        {
            for (int i = _size; i >= pos; i--)
            {
                _str[i + len] = _str[i];
            }
        }
        while (len)
        {
            _str[pos++] = *str++;
            len--;
        }
        _size += len;
    }

    int Find(char ch)
    {
        int i = 0;
        while (*_str != '\0')
        {
            if (*_str == ch)
            {
                cout << "find " << ch << endl;
                return i;
            }
            _str++;
            i++;
        }
        return -1;
    }

    int FindStr(const char* str)
    {
        int i, j, k;
        assert(str);
        for ( i = 0; i < strlen(_str); i++)
        {
            for ( k = i,j = 0; j < strlen(str); k++, j++)
            {
                if (_str[k] != str[j])
                {
                    break;
                }

            }
            if (str[j] == '\0' && k > 0)
            {
                cout << "find " << str << endl;
                return 1;
            }
        }
        return 0;
    }
public:
    bool operator<( String & s)
    {
        while (*_str && *s._str)
        {
            if (*_str < *s._str)
            {
                return true;
            }
            if (*_str == *s._str)
            {
                _str ++ ;
                s._str++;
            }
            else
            {
                return false;
            }
        }
        if (*_str)
        {
            return false;
        }
        else if (*s._str)
        {
            return true;
        }
        return false;
    }

    bool operator==( String & s)
    {
        while (*_str && *s._str)
        {
            if (*_str == *s._str)
            {
                _str++;
                s._str++;
            }
            else
            {
                return false;
            }
        }
        if (*_str || *s._str)
        {
            return false;
        }
        return true;
    }

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

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

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

    String operator+(const String& s)
    {
        InsertStr(_size, s._str);
        _size += strlen(s._str);
        return *this;
    }
private:

    void CheekCapacity(size_t capacity)
    {
        if (_capacity < capacity)
        {
            _capacity *= capacity;
            char* tmp = new char[_capacity];
            strcpy(tmp, _str);
            delete[] _str;
            _str = tmp;
        }
    }
    friend ostream& operator<<(ostream& out, const String& s);
private:
    char* _str;
    size_t _size;
    size_t _capacity;
};

ostream& operator<<(ostream& out, const String& s)
{
    out << s._str;
    return out;
}

void Test()
{
    String s1("abcd");
    s1.PushBack('e');
    s1.PushBack('f');
    s1.PushBack('g');
    cout << s1<<endl;
    s1.PopBack();
    s1.PopBack();
    s1.PopBack();
    s1.PopBack();
    //s1.PopBack();
    cout << s1<<endl;
}

void Test2()
{
    String s1("abcd");
    s1.InsertStr(4, "yangrujing");
    cout << s1;
    s1.Find('f');
}

void Test3()
{
    String s1("yangrujing");
    String s2("ru");
    //cout << "s1 < s1 ? : " <<(s1 < s2)<< endl;
    //cout << "s1 = s1 ? : " << (s1 == s2) << endl;
    //cout << "s1 >= s1 ? : " << (s1 >= s2) << endl;
    //cout << "s1 <= s1 ? : " << (s1 <= s2) << endl;
    //cout << "s1 > s1 ? : " << (s1 > s2) << endl;
    //cout << "s1 + s2 = " << s1 + s2 << endl;
    s1.FindStr("jing");
}
int main()
{
    /*String s1("abcd");
    String s2;
    s2 = s1;
    cout << s2;*/
    Test3();
    getchar();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值