String的实现涉及很多C++的基础知识、内存控制及异常处理等问题,
仔细研究起来非常复杂,本文主要做一个简单介绍和讲解模拟实现
String类的增删查改。
代码如下:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <assert.h>
using namespace std;
class String
{
public:
String(char* str = "")
{
_size = strlen(str);
_capacity = _size;
_str = new char[strlen(str)+1];
strcpy(_str,str);
}
void Swap(String& s)
{
swap(_str,s._str);
swap(_size,s._size);
swap(_capacity,s._capacity);
}
String(const String& s)
:_str(NULL)
,_size(0)
,_capacity(0)
{
String tmp(s._str);
Swap(tmp);
}
String& operator=(String s)
{
Swap(s);
return *this;
}
~String()
{
if (_str)
{
delete[] _str;
}
}
char* Getstr()
{
return _str;
}
size_t Size()
{
return _size;
}
size_t Capacity()
{
return _capacity;
}
void Expand(size_t n)
{
if (n > _capacity)
{
_str = (char*)realloc(_str,n+1);
assert(_str);
_capacity = n;
}
}
void PushBack(char ch)
{
if (_size == _capacity)
{
Expand(_capacity * 2);
}
_str[_size] = ch;
++_size;
_str[_size] = '\0';
}
void PushBack(const char* str)
{
size_t len = strlen(str);
if (len + _size > _capacity)
{
Expand(len + _size);
}
strcpy(_str + _size,str);
}
void PopBack()
{
assert(_size);
_str[_size-1] = _str[_size];
_size--;
}
void Insert(int pos,char ch)
{
if (_size==_capacity)
{
Expand(_capacity*2);
}
int end = _size;
while (end >= (int)pos)
{
_str[end + 1] = _str[end];
--end;
}
_str[pos] = ch;
++_size;
}
void Insert(size_t pos, const 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;
if (subIndex == subLen)
{
return srcIndex;
}
}
subIndex = 0;
srcIndex++;
}
return -1;
}
char& operator[](size_t pos)
{
assert(pos<_size);
return _str[pos];
}
bool operator<(const String& s) const
{
size_t i = 0;
for (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 (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);
}
private:
size_t _size;
size_t _capacity;
char* _str;
};