本篇模擬實現String的增刪查改功能,採用現代寫法。
具體實現如下:
class String
{
public:
//構造函數
String(const char* str)
:_str(new char[strlen(str)+1])
{
_size = strlen(str);
_capacity = _size;
strcpy(_str, str);
}
//實現複用,將其寫爲函數
void Swap(String& s)
{
swap(_str, s._str);
swap(_size, s._size);
swap(_capacity, s._capacity);
}
//拷貝構造函數s2(s1)
String(const String& s)
:_str(NULL)
, _size(0)
, _capacity(0)
{
String tmp(s._str);//採用現代寫法
Swap(tmp);
}
//s2=s1
String& operator=( String s)
{
if (this != &s)
{
Swap(s);
}
return *this;
}
//析構函數
~String()
{
if (_str)
{
delete[] _str;
_str = NULL;
}
}
//增容
void Expand(size_t n)
{
char *newstr = new char[n + 1];//開空間
strcpy(newstr, _str);
delete[] _str;
_str = newstr;
_capacity = n;
}
//尾插字符
void PushBack(char c)
{
if (_size >= _capacity)//擴容
{
Expand(_capacity * 2);
}
_str[_size++] = c;
_str[_size] = '\0';
}
//尾插字符串
void PushBack(const char *str)
{
size_t len = strlen(str);
if (_size+len > _capacity)//擴容
{
Expand(len + _size);
}
strcpy(_size+_str, str);//拷貝字符串
_size += len;
}
//頭插字符
void PushFront(char c)
{
if (_size>=_capacity)//擴容
{
Expand(_capacity * 2);
}
//後移字符串中的字符
for (int i = _size; i >= 0; --i)
{
_str[i + 1] = _str[i];
}
_str[0] = c;
_size++;
}
//頭插字符串
void PushFront(const char *str)
{
size_t len = strlen(str);
if (_size + len > _capacity)//擴容
{
Expand(_size + len);
}
//後移字符串
for (int i = _size; i >= 0; --i)
{
_str[len + i] = _str[i];
}
//字符串拷貝
char *p = _str;
while (*str != '\0')
{
*p++ = *str++;
}
_size += len;
}
//任意位置插入字符
void Insert(size_t pos, char c)
{
assert(pos <= _size);
if (_size > _capacity)
{
Expand(_capacity * 2);
}
//移數據
for (int i = _size; i >= (int)pos; --i)
{
_str[i + 1] = _str[i];
}
_str[pos] = c;
_size++;
}
//任意位置插入字符串
void Insert(size_t pos, char *str)
{
size_t len = strlen(str);
if (_size + len > _capacity)//擴容
{
Expand(_size + len);
}
//後移數據
for (int i = _size; i >= (int)pos; --i)
{
_str[len + i] = _str[i];
}
//拷貝字符串
while (*str != '\0')
{
_str[pos++] = *str++;
}
_size += len;
}
//刪除長度爲n的字符串
void Erase(size_t pos, size_t len)
{
assert(pos < _size);
if (pos + len>=_size)//pos位置之後全爲0
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
}
String operator+(char c)
{
String tmp(*this);
tmp.PushBack(c);
return tmp;
}
String& operator+=(char c)
{
PushBack(c);
return *this;
}
String operator+(const char *str)
{
String tmp(*this);
tmp.PushBack(str);
return tmp;
}
String& operator+=(const char *str)
{
PushBack(str);
return *this;
}
//s1>s2
bool operator>(const String& s) const
{
const char* p1 = _str;
const char* p2 = s._str;
while (*p1&&*p2)
{
if (*p1 > *p2)
{
return true;
}
else if (*p1 > *p2)
{
return false;
}
else
{
++p1;
++p2;
}
}
if (*p1)
return true;
if (*p2)
return false;
}
bool operator==(const String& s) const
{
const char *p1 = _str;
const char *p2 = s._str;
while (*p1&&*p2)
{
if (*p1 != *p2)
{
return false;
}
else
{
++p1;
++p2;
}
}
if (*p1 != '\0'&&*p2 != '\0')
{
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);
}
//查找字符
size_t Find(char c, size_t pos)
{
for (size_t i = pos; i < _size; ++i)
{
if (_str[i] == c)
return i;//找到,返回下標
}
return -1;//未找到
}
//查找字符串
size_t Find(const char* sub, size_t pos)
{
assert(sub);
assert(pos <_size);
const char* src = _str + pos;
while (*src)
{
const char* match = sub;//如果不匹配,返回子串起始處重新查找
const char *cur = src;
while (*match&&*match == *cur)//結束條件
{
++match;
++cur;
}
if (*match == '\0')//找到子串
{
return src - _str;//返回下標
}
else
{
++src;
}
}
return -1;//未找到
}
const char *c_str()
{
return _str;
}
//獲取指定位置的字符
char& operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];
}
const char& operator[](size_t pos)const
{
assert(pos < _size);
return _str[pos];
}
private:
char *_str;
size_t _size;
size_t _capacity;
};
//測試部分
void Test()
{
String s1("hello world");
//s1.PushFront('J');
//cout << s1.c_str() << endl;
//String s2 = s1 + "123";
//s2.PushFront("!!!!");
//cout << s2.c_str()<< endl;
s1 += 'x';
//s1 +="wc";
cout << s1.c_str() << endl;
cout << s1.Find('w', 2) << endl;
s1.Erase(1, 3);
cout << s1.c_str() << endl;
s1[0] = 'Q';
cout << s1.c_str() << endl;
String s3("change");
const String s4(s3);
cout << s4[3] << endl;
}
結果如下: