模拟String的增删查改

String的实现涉及很多C++的基础知识、内存控制及异常处理等问题,

仔细研究起来非常复杂,本文主要做一个简单介绍和讲解模拟实现

String类的增删查改。

代码如下:

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <assert.h>
using namespace std;
class String
{
public:
    String(char* str = "")
    {
        _size = strlen(str);
        _capacity = _size;
        _str = new char[strlen(str)+1];
        strcpy(_str,str);
    }
    void Swap(String& s)
    {
        swap(_str,s._str);
        swap(_size,s._size);
        swap(_capacity,s._capacity);
    }
    String(const String& s)
        :_str(NULL)
        ,_size(0)
        ,_capacity(0)
    {
        String tmp(s._str);
        Swap(tmp);
    }
    String& operator=(String s)
    {
        Swap(s);
        return *this;
    }
    ~String()
    {
        if (_str)
        {
            delete[] _str;
        }
    }
    char* Getstr()
    {
        return _str;
    }
    size_t Size()
    {
        return _size;
    }
    size_t Capacity()
    {
        return _capacity;
    }
    void Expand(size_t n)
    {
        if (n > _capacity)
        {
            _str = (char*)realloc(_str,n+1);
            assert(_str);
            _capacity = n;
        }
    }
    void PushBack(char ch)
    {
        if (_size == _capacity)
        {
            Expand(_capacity * 2);
        }
        _str[_size] = ch;
        ++_size;
        _str[_size] = '\0';
    }
    void PushBack(const char* str)
    {
        size_t len = strlen(str);
        if (len + _size > _capacity)
        {
            Expand(len + _size);
        }
        strcpy(_str + _size,str);
    }
    void PopBack()
    {
        assert(_size);
        _str[_size-1] = _str[_size];
        _size--;
    }
    void Insert(int pos,char ch)
    {
        if (_size==_capacity)
        {
            Expand(_capacity*2);
        }
        int end = _size;
        while (end >= (int)pos)
        {
            _str[end + 1] = _str[end];
            --end;
        }
        _str[pos] = ch;
        ++_size;
    }
    void Insert(size_t pos, const char* str)
    {
        size_t len = strlen(str);
        if (_size+len>_capacity)
        {
            Expand(_size+len);
        }
        int end = _size;
        while (end >= (int)pos)
        {
            _str[end + len] = _str[end];
            --end;
        }
        while (*str)
        {
            _str[pos++] = *str++;
        }
        _size += len;
    }
    void Erase(size_t pos, size_t count)
    {
        if (pos+count >= _size)
        {
            _str[pos] = '\0';
            _size = pos;
        }
        else
        {
            strcpy(_str + pos, _str + pos + count);
            _size -= count;
        }
    }
    size_t Find(char ch) const
    {
        size_t i = 0;
        for (i = 0;i<_size;i++)
        {
            if (_str[i]==ch)
            {
                return i;
            }
        }
        return -1;
    }
    size_t Find(const char* str) const
    {
        assert(str);
        const char* srcStr = _str;
        const char* subStr = str;
        size_t srcIndex = 0;
        size_t subIndex = 0;
        size_t subLen = strlen(subStr);
        while (srcIndex < _size)
        {
            size_t matchIndex = srcIndex;
            while (srcStr[matchIndex] == subStr[subIndex])
            {
                ++matchIndex;
                ++subIndex;
                if (subIndex == subLen)
                {
                    return srcIndex;
                }
            }
            subIndex = 0;
            srcIndex++;
        }
        return -1;
    }
    char& operator[](size_t pos)
    {
        assert(pos<_size);
        return _str[pos];
    }
    bool operator<(const String& s) const
    {
        size_t i = 0;
        for (i = 0;i < _size && i < s._size;i++)
        {
            if (_str[i] < s._str[i])
            {
                return true;
            }
            else if (_str[i]>s._str[i])
            {
                return false;
            }
        }
        if (i==_size)
        {
            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)||(*this==s);
    }
    bool operator==(const String& s) const
    {
        size_t i = 0;
        for (i = 0;i < _size && i < s._size;i++)
        {
            if (_str[i] != s._str[i])
            {
                return false;
            }
        }
        if (i == _size&&i == s._size)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    bool operator!=(const String& s)const
    {
        return !(*this==s);
    }
private:
    size_t _size;
    size_t _capacity;
    char* _str;
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值