简单实现string

标准库类型string表示可变长的字符序列,使用string类型必须首先包含string头文件,string定义在命名空间std中。我们可以自己简单实现string类。

namespace Dp
{
    class string
    {
        friend ostream& operator<<(ostream& _cout, const Dp::string& s)
        {
            for (size_t i = 0; i < s.size(); i++)
            {
                _cout << s[i];
            }
            return _cout;
        }
        friend istream& operator>>(istream& _cin, Dp::string& s)
        {
            s.clear();
            char ch;
            ch = _cin.get();
            const size_t N = 32;
            char buff[N];
            size_t i = 0;
            while (ch != ' ' && ch != '\n')
            {
                buff[i++] = ch;
                if (i == N - 1)
                {
                    buff[i] = '\0';
                    s += buff;
                    i = 0;
                }
                ch = _cin.get();
            }
            buff[i] = '\0';
            s += buff;
            return _cin;
        }
    public:
        // const static 语法特殊处理
        // 直接可以当成定义初始化
        static const size_t npos = -1;
        typedef char* iterator;
        typedef const char* const_iterator;
        string(const char* str = "")
        {
            _size = strlen(str);
            _capacity = _size;
            _str = new char[_size + 1];
            strcpy(_str, str);
        }
       /* string(const string& s)
            :_str(new char[s._size + 1])
            ,_size(s._size)
            ,_capacity(s._capacity)
        {                          
            strcpy(_str, s._str);           
        }*/
        string(const string& s)
            :_str(nullptr)
            , _size(0)
            , _capacity(0)
        {
            string tmp(s._str);
            swap(tmp); //this->swap(tmp);
        }
        void swap(string& s)
        {
           
            std::swap(_str, s._str);
            std::swap(_size, s._size);
            std::swap(_capacity,s._capacity);
        }
        string& operator=(const string& s)
        {          
            if (this != &s)
            {
                string  tmp(s);
                swap(tmp);
            }
            return *this;
        }
        ~string()
        {
            delete[] _str;
            _str = nullptr;
            _size = _capacity = 0;
        }
            //
            // iterator
        iterator begin() 
        {
            return _str;
        }
        iterator end()
        {
            return _str + _size;
        }
        const_iterator begin() const
        {
            return _str;
        }
        const_iterator end() const
        {
            return _str + _size;
        }
            /
            // modify
        void push_back(char c)
        {
            if (_size == _capacity)
            {
                reserve(_capacity == 0 ? 4 : _capacity * 2);
            }

            _str[_size] = c;
            ++_size;
            _str[_size] = '\0';
        }
        string& operator+=(char c)
        {
            push_back(c);
            return *this;
        }
        string& operator+=(const char* str)
        {
            insert(_size, str);
            return *this;
        }
        void clear()
        {
            _str[0] = '\0';
            _size = 0;
        }
        const char* c_str()const
        {
            return _str;
        }
        /
        // capacity

        size_t size()const
        {
            return _size;
        }
        size_t capacity()const
        {
            return _capacity;
        }
        bool empty()const
        {
            return _size == 0;           
        }
        void resize(size_t n, char c = '\0')
        {         
            if (n > _size)
            {
                // 插入数据
                reserve(n);
                for (size_t i = _size; i < n; ++i)
                {
                    _str[i] = c;
                }
                _str[n] = '\0';
                _size = n;
            }
            else
            {    // 删除数据
                _str[n] = '\0';
                _size = n;
            }
        }
        void reserve(size_t n)
        {
            if (n > _capacity)
            {
                char* tmp = new char[n + 1];
                strcpy(tmp, _str);
                delete[] _str;

                _str = tmp;
                _capacity = n;
            }
        }
        /
        // access
        char& operator[](size_t index)
        {
            if (index > _size)
            {
                cout << "please give  correct index" << endl;
                exit(-1);
            }
            return _str[index];
        }
        const char& operator[](size_t index)const
        {
            if (index > _size)
            {
                cout << "please give  correct index" << endl;
                exit(-1);
            }
            return _str[index];
        }
        /
        //relational operators
        bool operator>(const string& s)
        {
            return strcmp(_str, s._str) > 0;
        }   
        bool operator==(const string& s)
        {
            return strcmp(_str, s._str) == 0;
        }
        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);
        }
        // 返回c在string中第一次出现的位置
        size_t find(char c, size_t pos = 0) const
        {
            for (size_t i = pos; i < _size; i++)
            {
                if (_str[i] == c)
                {
                    return i;
                }
            }
            return npos;
        }
        // 返回子串s在string中第一次出现的位置
        size_t find(const char* s, size_t pos = 0) const
        {
       
            if (pos > _size)
            {
                cout << "error" << endl;
                exit(-1);
            }        
            const char* ptr = strstr(_str + pos, s);
            if (ptr == nullptr)
            {
                return npos;
            }
            else
            {
                return ptr - _str;
            }
        }
        // 在pos位置上插入字符c/字符串str,并返回该字符的位置
        string& insert(size_t pos, char c)
        {
            if (_size == _capacity)
            {
                reserve(_capacity == 0 ? 4 : _capacity * 2);
            }
            size_t end = _size + 1;
            while (end > pos)
            {
                _str[end] = _str[end - 1];
                --end;
            }
            _str[pos] = c;
            ++_size;
            return *this;          
        }
        string& insert(size_t pos, const char* str)
        {        
            size_t len = strlen(str);
            if (_size + len > _capacity)
            {
                reserve(_size + len);
            }
            // 挪动数据
            size_t end = _size + len;
            while (end >= pos + len)
            {
                _str[end] = _str[end - len];
                --end;
            }

            strncpy(_str + pos, str, len);
            _size += len;

            return *this;
        }
        void erase(size_t pos, size_t len = npos)
        {
            if (len == npos || pos + len >= _size)
            {
                _str[pos] = '\0';
                _size = pos;
            }
            else
            {
                strcpy(_str + pos, _str + pos + len);
                _size -= len;
            }
       }
        string substr(size_t pos, size_t len = npos) const
        {
            if (pos > _size)
            {
                cout << "error" << endl;
                exit(-1);
            }
            size_t realLen = len;
            if (len == npos || pos + len > _size)
            {
                realLen = _size - pos;
            }

            string sub;
            for (size_t i = 0; i < realLen; ++i)
            {
                sub += _str[pos + i];
            }

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值