String的增删查改的实现

String类主要模拟实现string(库)中字符串的增删查改操作。有插入一个字符,插入一个字符串,删除一个特定位置的字符,删除特定位置特定的字符串,头插,头删,尾插,尾删,可以复用Insert()和Erase()函数。字符串比较,字符相同则同时向后走,当字符串相同,或有不同的字符时返回相应的结果。具体操作如下:

#pragma warning(disable:4996)//禁止报strcpy的错误
#include<iostream>
#include<cassert>
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;
    }
    //s1(s2)
    String(const String& s)
        :_str(NULL)
        ,_size(0)
        ,_capacity(0)
    {
       String tmp(s._str);
       Swap(tmp);
    }
    //d1=d2
    String& operator=(String s)//现代写法
    {
       this->Swap(s);
       return *this;
    }

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

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

    char* GetStr()
    {
        return _str;
    }

    size_t Size()
    {
       return _size;
    }

    size_t Capacity()
    {
       return _capacity;
    }

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

    void Expand(size_t n)
    {
       assert(n > _capacity);
       _str = (char*)realloc(_str,n+1);
       _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(_size+len > _capacity)
        {
            Expand(_size+len);
        }
        strcpy(_str+_size,str);
    }

    void PopBack()
    {
        if(_size)
        {
            --_size;
            _str[_size] = '\0';
        }
    }

    void PushFront(char ch)
    {
        Insert(0,ch);
    }

    void PushFront(const char* str)
    {
        Insert(0,str);
    }

    void PopFront()
    {
        if(_str)
        {   
            strcpy(_str,_str+1);
            --_size;
        }
    }

这里写图片描述

    void Insert(size_t pos, char ch)//在pos前面插入一个字符
    {
        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)//在pos前面插入一个字符串
    {
        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 = _size + len;
    }

    void Erase(size_t pos, size_t count)
    {
        if(pos+count >= _size-1)
        {
            _str[pos] = '\0';
            _size = pos;
        }
        else
        {
            strcpy(_str+pos, _str+pos+count);
            //把pos+count之后的内容拷贝到pos后
            _size -= count;
        }
    }

这里写图片描述

    int Find(char ch) const
    {
        for(size_t i=0; i<_size; ++i)
        {
            if(_str[i] == ch)
                return i;
        }
        return -1;
    }

    int Find(const char* str) const
    {
        assert(str);
        const char* srcStr = _str;
        const char* subStr = str;
        size_t subLen = strlen(str);
        size_t srcIndex = 0;
        size_t subIndex = 0;

        while(srcIndex < _size)
        {
            size_t matchIndex = srcIndex;
            while(srcStr[matchIndex] == subStr[subIndex])
            {
                ++matchIndex;
                ++subIndex;
                if(subIndex == subLen)
                    return srcIndex;
            }
            subIndex = 0;
            srcIndex++;
        }
    return -1;
    }

    bool operator<(const String& s)
    {
        size_t i=0;
        for( ;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 && i<s._size)//i是已经for循环过的值
            return true;
        else
            return false;
    }

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

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

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

    bool operator==(const String& s) 
    {
        size_t i =0;
        for(; 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) 
    {
        return !(*this == s);
    }

private:
    char* _str;
    size_t _size;//字符个数
    size_t _capacity;//容量空间
};

测试用例:

#include"String.h"

void TestString1()
{
    String s1("hello");
    String s2(s1);
    //cout<<s1.GetStr()<<endl;
    //cout<<s2.GetStr()<<endl;

    s1.PushBack(' ');
    s1.PushBack('C');
    s1.PushBack('h');
    s1.PushBack('i');
    s1.PushBack('n');
    s1.PushBack('a');
    cout<<s1.GetStr()<<endl;

    /*s2.PushBack(" China");
    cout<<s2.GetStr()<<endl;*/

    s1.PushFront(' ');
    s1.PushFront('m');
    cout<<s1.GetStr()<<endl;

    /*s2.PopBack();
    s2.PopBack();
    s2.PopBack();
    s2.PopBack();
    s2.PopBack();
    s2.PopBack();*/
   // cout<<s2.GetStr()<<endl;

    s2.PopFront();
    s2.PopFront();
    s2.PopFront();
    s2.PopFront();
    s2.PopFront();
    s2.PopFront();
    cout<<s2.GetStr()<<endl;

    String s3("world");
    s3.Insert(0,"hello ");
    s3.Insert(6,'m');
    cout<<s3.GetStr()<<endl;
    //s3.Erase(7,2);
    //cout<<s3.GetStr()<<endl;

    cout<<"Pos"<<s3.Find('w')<<endl;
    cout<<"Pos"<<s3.Find("wor")<<endl;
}

void TestString2()
{
    String s1("computer");
    String s2("compare");
    String s3("computerly");
    String s4("computer");
    bool ret;
    //ret = s1<s2;
    //cout<<"s1<s2 ? "<<ret<<endl;

    //ret = s1<s3;
    //cout<<"s1<s3 ? "<<ret<<endl;

    /*ret = s1>s2;
    cout<<"s1 >s42? "<<ret<<endl;*/

    /*ret = s1>=s2;
    cout<<"s1 >= s2 ? "<<ret<<endl;*/

    ret = s1<=s2;
    cout<<"s1 <= s2 ? "<<ret<<endl;


}
int main()
{
    //TestString1();
    TestString2();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值