模拟实现string——增删查改

来源:互联网 发布:log4j.xml 打印sql 编辑:程序博客网 时间:2024/04/29 14:21

我们上篇和上上篇谈到了模拟实现string——深浅拷贝以及写时拷贝
这篇介绍一下增删查改

首先介绍一下它 的接口

void PushBack(char ch) void PushBack(const char* str) void PopBack() void Insert(size_t pos, char ch) void Insert(size_t pos, const char* str); void Erase(size_t pos, size_t count); size_t Find(char ch) const; size_t Find(const char* str) const; char& operator[](size_t pos) bool operator<(const String& s) const; bool operator<=(const String& s) const; bool operator>(const String& s) const; bool operator>=(const String& s) const; bool operator==(const String& s) const; bool operator!=(const String& s)const; void Expand(size_t n) 

下面是源代码

#include<iostream>#include<assert.h>#include<windows.h>using namespace std;#pragma warning(disable :4996)class String{public:    String(char* str = "")//无参函数也可用,构造函数    {        _size = strlen(str);        _capacity = _size;        _str = new char[strlen(str) + 1];        strcpy(_str, str);    }    //String(const String &s)     //传统写法,拷贝构造    //  :_str(new char[strlen(s._str)+1])    //{    //  strcpy(_str, s._str);    //}    String(const String &s)    //现代写法,拷贝构造        :_str(NULL)        , _capacity(0)        , _size(0)    {        String tmp(s._str);        Swap(tmp);    }    char &operator[](size_t pos)    {        return _str[pos];    }    //String &operator=(const String &s)  //两种写法    //{    //  if (this != &s)    //  {    //      String tmp(s._str);    //      Swap( tmp);    //  }    //  return *this;    //}    String& operator=(String s)     {        Swap(s);        return *this;    }    const char* GetStr()    {        return _str;    }    size_t Size()    {        return _size;    }    size_t Capacity()    {        return _capacity;    }    void Swap(String& s)    {        swap(_capacity, s._capacity);        swap(_size, s._size);        swap(_str, s._str);    }    ~String()  //析构函数    {        delete[] _str;    }//增删查改    void PushBcak(char ch)   //尾插字符    {        if (_capacity == _size)        {            Expand(2 * _size);        }        _str[_size++] = ch;        _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()   //尾删    {        assert(_size);        _str[_size - 1] = _str[_size];        _size--;    }    void Insert(size_t pos, char ch)  //添加字符    {        if (_size == _capacity)        {            Expand(_size * 2);        }        int end = _size;        while (end >= int(pos))        {            _str[end + 1] = _str[end];            --end;        }        _str[pos] = ch;        _size++;    }    void Insert(size_t pos, 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;                return srcIndex;                if (subIndex = subLen)                {                    return srcIndex;                }            }            subIndex = 0;            srcIndex++;        }        return -1;    }    bool operator<(const String &s)const    {        size_t i = 0;        for (size_t 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 (size_t 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);    }    void Expand(size_t n)    {        if (n > _capacity)        {            _str = (char*)realloc(_str, n + 1);            assert(_str);            _capacity = n;        }    }private:    char *_str;    size_t _capacity;    size_t _size;};
原创粉丝点击