模拟实现string类

class string
{
public:
//构造函数
 string(const char* str = "")
 {
  if (str == nullptr)
   str = "";
  _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(nullptr)
 {
  string tmp(s._str);
  swap(_str, tmp._str);
 }

//赋值运算符重载
 //传统写法
 string& operator=(const string &s)
 {
  if (this != &s)
  {
   delete[] _str;
   _str = new char[strlen(s._str) + 1];
   strcpy(_str, s._str);
  }
  return *this;
 }

 //现代写法
 string& operator=(const string &s)
 {
  if (this != &s)
  {
   string tmp(s);
   swap(_str, tmp._str);
  }
  return *this;
 }

//析构函数
 ~string()
 {
  if (_str)
  {
   delete[] _str;
   _str = nullptr;
  }
 }

private:
 char* _str;
};
namespace bit
{
 class string
 {
  friend ostream& operator<<(ostream& _cout, const bit::string& s);
  friend istream& operator>>(istream& _cin, bit::string& s);
 public:
  typedef char* iterator;
   public:
   //构造函数
  string(const char* str = "")
  {
   _size = strlen(str);
   _capacity = _size;
   _str = new char[_capacity + 1];
   strcpy(_str, str);
  }

//拷贝构造
 string(const string& s)
   :_str(nullptr),
    _size(0),
    _capacity(0)
  {
   string tmp(s._str);
   this->swap(tmp);
  }

//赋值运算符重载
  string& operator=(const string &s)
  {
   if (this != &s)
   {
    string tmp(s);
    this->swap(tmp);
   }
   return *this;
  }

//析构函数
 ~string()
  {
   if (_str)
   {
    delete[] _str;
    _str = nullptr;
   }
  }

  //
   // iterator
  iterator begin()
  {
   return _str;
  }

 iterator end()
  {
   return _str + _size;
  }

   /
   // modify
   //尾插
     void PushBack(char c)
  {
   if (_size == _capacity)
   {
    size_t newC = _capacity == 0 ? 1 : 2 * _capacity;
    reserve(newC);
    _str[_size] = c;
    _size++;
    _str[_size] = '\0';
   }
  }

  string& operator+=(char c)
  {
   PushBack(c);
   return *this;
  }

//尾插字符串
  void Append(const char* str)
  {
   int len = strlen(str);
   if (_size + len > _capacity)
   {
    reserve(_size + len);
   }
   strcpy(_str, str);
   _size += len;
  }

  string& operator+=(const char* str)
  {
   Append(str);
   return *this;
  }
  
  void clear()
  {
   _size = 0;
   _str[_size] = '\0';
  }

  void swap(string& s)
  {
   std::swap(_str, s._str);
   std::swap(_size, s._size);
   std::swap(_capacity, s._capacity);
  }

  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 newSize, char c = '\0')
  {
   if (newSize > _size)
   {
    if (newSize > _capacity)
    {
     reserve(newSize);
    }
    memset(_str + _size, c, newSize - _size);
   }
   _size = newSize;
   _str[_size] = '\0';
  }

  void reserve(size_t newCapacity)
  {
   if (newCapacity > _capacity)
   {
    char* str = new char[newCapacity + 1];
    strcpy(str, _str);
    delete[] _str;
    _str = str;
    _capacity = newCapacity;
   }
  }

  /
  // access
  char& operator[](size_t index)
  {
     assert(index < _size);
   return _str[index];
  }
  const char& operator[](size_t index)const
  {
   assert(index < _size);
   return _str[index];
  }

  /
  //relational operators
  bool operator<(const string& s)
  {
     int res = strcmp(_str, s._str);
   if (res < 0)
    return true;
   return false;
  } 

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

  bool operator>(const string& s)
  {
   int res = strcmp(_str, s._str);
   if (res > 0)
    return true;
   return false;
  }

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

  bool operator==(const string& s)
  {
   int res = strcmp(_str, s._str);
   if (res == 0)
    return true;
   return false;
  }

  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 -1;
  }

  // 返回子串s在string中第一次出现的位置
  size_t find(const char* s, size_t pos = 0) const
  {
   assert(s);
   assert(pos < _size);
   const char* src = _str + pos;
   while (*src)
      {
    const char* match = s;
    const char* cur = src;
    while (*match && *match == *cur)
    {
     ++match;
     ++cur;
    }
    if (*match == '\0')
    {
         return src - _str;
    }
    else
    {
     src++;
    }
   }
   return -1; 
  }

  // 在pos位置上插入字符c/字符串str,并返回该字符的位置
  string& insert(size_t pos, char c)
  {
   if (pos > _size)
    return;
   if (_size == _capacity)
   {
       size_t newC = _capacity == 0 ? 15 : 2 * _capacity;
    reserve(newC);
   }
   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)
  {
   if (pos > _size)
    return;
   int len = strlen(str);
   if (_size + len > _capacity)
   {
       reserve(_size + len);
   }
   size_t end = _size + len;
   while (end > pos + len - 1)
   {
    _str[end] = _str[end - len];
    --end;
       }
   for (int i = 0; i < len; ++i)
   {
    _str[pos + i] = str[i];
   }
   _size += len;
   return *this;
  }

  // 删除pos位置上的元素,并返回该元素的下一个位置
  string& erase(size_t pos, size_t len)
  {
   if (pos < _size)
   {
       if (pos + len >= _size)
    {
     _size = pos;
     _str[_size] = '\0';
    }
    else
    {
         for (int i = pos + len; i <= _size; i++)
     {
      _str[pos++] = _str[i];
     }
     _size -= len;
    }
   }
   return *this;
  }

 private:
  char* _str;
  size_t _capacity;
  size_t _size;
 }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值