说明:有些函数返回的是String&(引用),有些是String(值),这取决于返回的对象是函数的局部变量还是全局变量。
String.hpp
#include <string>
#include <iostream>
#include <assert.h>
using namespace std;
class String
{
public:
String(const char* str = "")
{
if (nullptr == str)
str = "";
_size = strlen(str);
_capacity = _size + 1;
_str = new char[_size + 1];
strcpy(_str, str);
}
String(const String& s)
:_size(s._size)
, _capacity(s._capacity)
{
_str = new char[_size + 1];
strcpy(_str, s._str);
}
void Swap(String& s);
String& operator=(String s);
~String()
{
if (nullptr != _str)
{
delete[] _str;
_str = nullptr;
}
}
void Show();
const char* c_Str();//获取String类中的C风格字符串
const char& operator[](size_t index)const;//括号运算符
void Expand(size_t n);//扩容
void PushBack(char c);//插入一个字符
void PushBack(const char* str);//插入一个字符串
void PopBack();//尾部删除一个字符
void Insert(size_t pos, char c);//pos位置插入一个字符
void Insert(size_t pos, const char* str);//pos位置插入一个字符串
void Erase(size_t pos, size_t n = 1);//在pos位置删除n个字符串
size_t Find(char c);//返回String对象中第一次出现字符c的下标
size_t Find(const char* str);//返回String对象中第一次出现str的下标
String operator+(char c);
String& operator+=(char c);
String operator+(const char* str);
String& operator+=(const char* str);
bool operator>(const String& s);
bool operator>=(const String& s);
bool operator<(const String& s);
bool operator<=(const String& s);
bool operator==(const String& s);
bool operator!=(const String& s);
private:
char* _str;
size_t _size;
size_t _capacity;
};
String.cpp
void String::Show()
{
cout << "_str" << _str << endl;
}
void String::Swap(String& s)
{
_size = s._size;
_capacity = s._capacity;
delete[] _str;
_str = s._str;
s._str = new char[1];
}
String& String::operator=(String s)
{
Swap(s);
return *this;
}
const char* String::c_Str()
{
return _str;
}
void String::Expand(size_t n)
{
char* tmp = new char[n];
memcpy(tmp, _str, _size + 1);
delete[] _str;
_str = tmp;
_capacity = n;
}
void String::PushBack(char c)
{
Insert(_size, c);
}
void String::PushBack(const char* str)
{
Insert(_size, str);
}
void String::Insert(size_t pos, char c)
{
assert(_size > pos);
if (_size >= _capacity - 1)
{
Expand(2 * _capacity + 1);
}
for (int i = _size; i > pos; i--)
{
_str[i] = _str[i - 1];
}
_str[pos] = c;
++_size;
}
void String::Insert(size_t pos, const char* str)
{
assert(pos < _size);
const char* tmp = str;
size_t size = 0;
for (; *tmp != '\0'; tmp++)
{
++size;
}
if (_capacity < _size + size)
{
Expand(_capacity + size);
}
for (int i = _size; i >= pos; i--)
{
_str[i + size] = _str[i];
}
int i = 0;
while (*(str + i) != '\0')
{
_str[pos + i] = str[i];
++i;
}
_size = _size + size;
}
void String::PopBack()
{
Erase(_size - 1, 1);
}
void String::Erase(size_t pos, size_t n)
{
if (0 == _size)
{
return;
}
if (0 == n)
{
return;
}
assert(pos <= _size);
while ((pos + n) <= _capacity)
{
_str[pos] = _str[pos + n];
++pos;
}
if (_size - n > 0)
{
_size = _size - n;
}
else
{
_size = 0;
}
_str[_size] = 0;
}
size_t String::Find(char c)
{
for (size_t i = 0; i < _size; i++)
{
if (_str[i] == c)
{
return i;
}
}
return (size_t)-1;
}
size_t String::Find(const char* str)
{
const char* _fast = _str;
const char* _slow = _str;
const char* cur = str;
while (_slow < (_str + _size))
{
_fast = _slow;
if (*_fast == *cur)
{
while (*_fast == *cur)
{
if (*_fast == '\0')
{
break;
}
cur++;
_fast++;
}
if (*cur == '\0')
{
return _slow - str;
}
}
_slow++;
cur = str;
}
return (size_t)-1;
}
String String::operator+(char c)
{
String tmp(*this);
tmp.PushBack(c);
return tmp;
}
String& String::operator+=(char c)
{
this->PushBack(c);
return *this;
}
String String::operator+(const char* str)
{
String tmp(*this);
tmp.PushBack(str);
return tmp;
}
String& String::operator+=(const char* str)
{
this->PushBack(str);
return *this;
}
bool String::operator>(const String& s)
{
const char* _tmp = _str;
const char* tmp = s._str;
while (*tmp != '\0' && *_tmp != '\0' && *tmp == *_tmp)
{
tmp++;
_tmp++;
}
if (*_tmp > *tmp)
{
return true;
}
else
{
return false;
}
}
bool String::operator>=(const String& s)
{
if (*this > s || *this == s)
{
return true;
}
return false;
}
bool String::operator<(const String& s)
{
const char* _tmp = _str;
const char* tmp = s._str;
while (*_tmp != '\0' && *tmp != '\0' && *_tmp == *tmp)
{
_tmp++;
tmp++;
}
if (*_tmp < *tmp)
{
return true;
}
return false;
}
bool String::operator<=(const String& s)
{
if (*this < s || *this == s)
{
return true;
}
else
{
return false;
}
}
bool String::operator==(const String& s)
{
const char* _tmp = _str;
const char* tmp = s._str;
while (*_tmp == *tmp)
{
if (*_tmp == '\0' && *tmp == '\0')
{
return true;
}
tmp++;
_tmp++;
}
return false;
}
bool String::operator!=(const String& s)
{
if (!(*this == s))
{
return true;
}
return false;
}