String类的模拟实现

#include<iostream>
#include<string>

using namespace std;

class String {
private:
   char* _str;
   size_t _size;
   size_t _capacity;
public:
   //string迭代器:通过指针实现
   typedef char* iterator;
   typedef const char* const_iterator;
   
   iterator begin() {
      //第一个元素的位置
      return _str;
   }

   iterator end() {
      //最后一个元素的下一个位置
      return _str + _size;
   }

   const_iterator begin() const {
      //第一个元素的位置
      return _str;
   }
   
   const_iterator end() const {
      //最后一个元素的下一个位置
      return _str + _size;
   }

   String()
       :_str(new char[16]),//多余一个位置存放'\0'
        _size(0),
        _capacity(0)
   {
      _str[_size] = '\0';
      _capacity = 15;
   }

   //含参构造
   String(const char* str) {
      _size = strlen(str);
      _str = new char[_size + 1];//多余一个位置存放'\0'
      strcpy(_str, str);
      _capacity = _size;
   }

   //析构函数
   ~String() {
      if (_str) {
      delete[] _str;
      _size = _capacity = 0;
      _str = nullptr;
      }
      cout << "~String" << endl;
   }
   
   //扩容
   void reserve(size_t n) {
      if (n > _capacity) {
      //开空间
      char* tmp = new char[n + 1];
      //拷贝
      strcpy(tmp, _str);
      //释放原有空间
      delete[] _str;
      //更新指向、容量
      _str = tmp;
      _capacity = n;
      }
   }
 
   //简单交换,没有开空间的操作,只交换成员和资源
   void Swap(String& str) {
      swap(_str, str._str);
      swap(_size, str._size);
      swap(_capacity, str._capacity);
   }

   //拷贝构造:现代写法,代码复用:构造函数
   String(const String& str)
       :_str(nullptr),
        _size(0),
        _capacity(0)
   {
      //调用构造函数
      String tmp(str._str);
      Swap(tmp);
   }

   //赋值运算符:现代写法,代码复用:拷贝构造(传参进行拷贝构造)
   String& operator=(String str) {
      Swap(str);
      return *this;
   }

   //字符串有效长度
   size_t size() const {
      return _size;
   }
   
   const char* c_str() const {
      return _str;
   }

   //尾插字符(实际可用insert实现)
   void pushBack(const char& ch) {
      //检查容量
      if (_size == _capacity) {
         size_t newC = _capacity==0 ? 15 : 2 * _capacity;
         reserve(newC);
      }
      //尾插
      _str[_size] = ch;
      //更新size
      _size++;
      _str[_size] = '\0';
   }

   //尾插字符串(实际可用insert实现)
   void Append(const char* str) {
      int len = strlen(str);//要插入字符串的长度
      //检查容量
      if (_size + len > _capacity) {
         reserve(_size + len);
      }
      //尾插
      strcpy(_str + _size, str);
      //更新size
      _size += len;
   }

   //+= 尾插字符(实际可用insert实现)
   String& operator+=(const char& ch) {
      pushBack(ch);
      return *this;
   }

   //+= 尾插字符串(实际可用insert实现)
   String& operator+=(const char* str) {
      Append(str);
      return *this;
   }

   //任意位置插入字符
   void insert(size_t pos, const char& ch) {
      //检查容量
      if (_size == _capacity) {
         size_t newC = _capacity == 0 ? 15 : 2 * _capacity;
         reserve(newC);
      }
      //移动元素[pos,_size]:从后向前移动,首先移动最右端的字符,防止覆盖
      size_t end = _size + 1;
      //end >= pos:当pos = 0时,会死循环,访问越界
      while (end > pos) {
         _str[end] = _str[end - 1];//不使用end + 1,防止访问越界
         end--;
      }
      //插入
      _str[pos] = ch;
      _size++;
   }

   //任意位置插入字符串
   void insert(size_t pos, const char* str) {
      if (pos > _size) {
         return;
      }
      int len = strlen(str);
      //检查容量
      if (_size + len > _capacity) {
         reserve(_size + len);
      }
      //移动元素[pos,_size]:从前向后移动,首先移动最右端字符,防止覆盖
      size_t end = _size + len;
      while (end > pos + len - 1) {
         _str[end] = _str[end - len];
         end--;
      }
      //插入
      for (int i = 0; i < len; i++) {
         _str[i + pos] = str[i];
      }
      //更新size
      _size += len;
   }
}

//迭代器遍历:可读可写操作
void EprintfString(String& str) {
   String::iterator it = str.begin();
   while (it != str.end()) {
      cout << *it << " ";
      *it = '0';
      it++;
   }
   cout << endl;
}

//迭代器遍历:只读操作
void printfString(const String& str){
   String::const_iterator it = str.begin();
   while (it != str.end()) {
      cout << *it << " ";
      it++;
   }
   cout << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值