C++ string 模拟

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<assert.h>
using namespace std;
namespace mystring
{
class string
{
public:
    //static int npos;
    typedef char* iterator;
    typedef const char* const_iterator;
    string(const char*s="")
    {
        _size = strlen(s);
        _capcity = _size;
        _ch= new char[_size+1];
        strcpy(_ch, s);
    }
    iterator begin()
    {
        return _ch;
    }
    iterator end()
    {
        return _ch + _size;
    }
    const_iterator cbegin() const 
    {
        return _ch;
    }
    const_iterator cend() const
    {
        return _ch + _size;
    }
    void reserve(int pos)
    {
        if (pos <= _size)
        {
            _size = pos;
            _ch[_size] = '\0';
        }
        else
        {
            char* tmp = new char[pos + 1];
            strcpy(tmp, _ch);
            _ch = tmp;
            _capcity = pos + 1;
        }
    }
    void resize(int pos, char s)
    {
        if (pos <= _size)
        {
            _size = pos;
            _ch[_size] = '\0';
        }
        else if(pos>_capcity)
        {
            reserve(pos);
            memset(_ch + _size, s, pos - _size);
            _size = pos;
            _ch[_size] = '\0';
        }
    }

    void Swap(string& s)
    {
        std::swap(_ch, s._ch);
        std::swap(_size, s._size);
        std::swap(_capcity, s._capcity);

    }
    //s1(s2)使用构造函数
    string(string& s)
        :_ch(nullptr)
        ,_size(0)
        ,_capcity(0)

    {
        string tmp(s._ch);
        Swap(tmp);
    }
    //s1=s2;
    //使用构造函数
    string& operator=(string& s)
    {
        Swap(s);
        return *this;
    }
    void push_back(char s)
    {
        if (_size == _capcity)
        {
            int newcapcity = _capcity == 0 ? 4: _capcity * 2;
            reserve(newcapcity);
         }
        _ch[_size] = s;
        _size++;
        _ch[_size] = '\0';
    }
    void pop_back()
    {
        _size--;
        _ch[_size] = '\0';
    }
    char& operator[](int pos)
    {
        assert(pos <= _size);
        return _ch[pos];
    }

     string& append(const char* s,int pos)
    {
        int len = strlen(s);
        if (_size+len >=_capcity)
        {
            int newcapcity = _capcity == 0 ? _size + len :len + _capcity;
            reserve(newcapcity);
        }
        int end = _size;
        
        while (end >= pos)
        {
            _ch[end + len] = _ch[end];
            if (end > 0)
            {
                end--;
            }
        }
        strncpy(_ch + pos, s, len);
        return *this;

    }
     string& insert(int pos,char s)
     {
         assert(pos < _size);
         if (_size+1 == _capcity)
         {
             int newcapcity = _capcity == 0 ? _size + 1 : 1 + _capcity;
             reserve(newcapcity);
         }
         int end = _size;
         while (end >=pos)
         {
             _ch[end + 1] = _ch[end];
             if (end > 0)
             {
                 end--;
             }

         }
         _ch[pos] = s;
         _size++;
         return *this;
     }
     string& erase(int pos = 0, int len =npos)
     {
         assert(pos < _size);
         if (pos==npos||pos+len>=_size)
         {
             _ch[pos] = '\0';
             _size = pos;
         }
         else
         {
             strncpy(_ch + pos, _ch + pos + len,len);
             _size -= len;
         }
         return *this;
         
     }

    const char* c_str()const //前const 表示返回常量,后面的const 表示返回值不能修改
    {
        return this->_ch;
    }

    ~string()
    {
        delete[] _ch;
        _ch = nullptr;
        _size = 0;
        _capcity = 0;
    }
private:
    char* _ch;
    int _size;
    int _capcity;
    static int npos;
    
};
int mystring::string::npos = -1;
void test()
{
    /*mystring::string s("laskdf");
    cout << s.c_str() << endl;
    mystring::string::iterator it = s.begin();
    while (it != s.end())
    {
        cout << *it<< endl;
        it++;
    }
    mystring::string s1(s);
    cout << s1.c_str();
    mystring::string s2("123");
    mystring::string s3("456");
    s2 = s3;
    cout << s2.c_str();*/
    string s4;
    s4.push_back('a');
    s4.push_back('a');
    s4.push_back('a');
    s4.push_back('a');
    //s4.append("love you",1);
//    cout << s4.c_str();
    s4.insert(1,'i');
    cout << s4.c_str();


    
}
    
}

int main()
{
mystring::test();
    return 0;
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值