class String
{
public:
String(const char* str = "")
:_size(strlen(str))
,_capacity(_size)
{
_str = new char[_size+1];
strcpy(_str,str);
}
//swap(s1,s2) ---> s1.swap(s2)
void Swap(String& s)
{
swap(_str,s._str);
swap(_size,s._size);
swap(_capacity,s._capacity);
}
//s2(s1)
String(const String& s)
:_str(NULL)
{
String tmp(s._str);
this->Swap(tmp);
}
//s1 = s3
String& operator = (String& s)
{
this->Swap(s);
return *this;
}
~String()
{
if(_str)
{
delete[] _str;
_str = NULL;
}
}
//Capacity
void Reverse(size_t n)
{
Expand(n);
}
//Size+Capacity
//初始化
void Resize(size_t n,char ch = '\0')
{
if(n < _size)
{
_size = n;
_str[_size] = '\0';
}
else
{
if(n > _capacity)
{
Expand(n);
}
for(size_t i = _size;i < n;i++)
{
_str[i] = ch;
}
_str[n] = '\0';
_size = n;
}
}
void Expand(size_t n)
{
if(n > _capacity)
{
char* tmp = new char[n+1];
strcpy(tmp,_str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
//插入一个字符
void PushBack(char ch)
{
// if(_size == _capacity)
// {
// Expand(_capacity*2);
// }
// _str[_size] = ch;
// _str[_size+1] = '\0';
// _size++;
Insert(_size,ch);
}
//添加一个字符串
void Append(const char* str)
{
// size_t len = strlen(str);
// if(_size + len > _capacity)
// {
// size_t newcapacity = _capacity * 2;
// while(newcapacity < _size + len)
// {
// newcapacity *= 2;
// }
// Expand(_size + len);
// }
// strcpy(_str+_size,str);
// _size += len;
Insert(_size,str);
}
//在pos位置插入一个字符
void Insert(size_t pos,char ch)
{
//检查pos是否越界
assert(pos <= _size);
//空间不够,扩容
if(_size == _capacity)
{
Expand(_capacity*2);
}
//将pos之后的所有元素都向后移动
size_t end = _size;
while(end != pos)
{
_str[end+1] = _str[end];
--end;
}
//在pos位置插入指定元素
_str[end] = ch;
++_size;
}
//在pos位置插入一个字符串
void Insert(size_t pos,const char* str)
{
assert(pos <= _size);
//检查pos是否越界
int len = strlen(str);
if(len + _size > _capacity)
{
//空间不够,扩容
Expand(_size+len);
}
int end = _size;
while(end >= (int)pos)
{
//将元素向后移动len个位置
_str[end+len] = _str[end];
--end;
}
//将要插入的字符串拷贝过来
strncpy(_str+pos,str,len);
_size += len;
}
//s1 += "world"
String& operator += (const char* str)
{
this->Append(str);
return *this;
}
//s1 += s2
String& operator += (const String s)
{
// this->Append(s._str);
*this += s._str;
//this->operator += (s._str);
return *this;
}
//s1 + "world"
String operator + (const char* str)
{
String ret(_str);
ret.Append(str);
return ret;
}
//s1 + s2
String operator + (const String s)
{
String ret(_str);
return *this + s._str;
}
void PopBack()
{
//检查是否为空
assert(_size > 0);
--_size;
_str[_size] = '\0';
}
//删除指定位置的len个元素
void Erase(size_t pos,size_t len)
{
assert(pos < _size);
//检查pos是否合法
//如果从pos位置往后的元素不够len个健康
//就把pos位置置为0
//否则,就用后面的元素覆盖掉前len个元素
if(pos + len >= _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str+pos,_str+pos+len);
_size -= len;
}
}
size_t Size()
{
return _size;
}
size_t Capacity()
{
return _capacity;
}
bool Empty()
{
return _size == 0;
}
//[]重载
char& operator[](size_t pos)
{
return _str[pos];
}
//查找一个指定的元素
size_t Find(char ch) const
{
for(size_t i = 0;i < _size;i++)
{
if(_str[i] == ch)
{
return i;
}
}
return npos;
}
//查找一个指定的字符串
size_t Find(const char* sub) const
{
char* cur = _str;
while(*cur)
{
const char* cur_tmp = cur;
const char* sub_tmp = sub;
while(*sub_tmp && *cur_tmp == *sub_tmp)
{
cur_tmp++;
sub_tmp++;
}
if(*sub_tmp == '\0')
{
return cur-_str;
}
else
{
cur++;
}
}
return npos;
}
//运算符重载
//s1 < s2 ---> s1.operator<(s2)
bool operator < (const String& s)const
{
const char* str1 = _str;
const char* str2 = s._str;
while(*str1 && *str2)
{
if(*str1 < *str2)
{
return true;
}
else if(*str1 > *str2)
{
return false;
}
else
{
str1++;
str2++;
}
}
if(*str1 == '\0' &&*str2 != '\0')
{
return true;;
}
else
{
return false;
}
}
bool operator == (const String s)const
{
const char* str1 = _str;
const char* str2 = s._str;
while(*str1 && *str2)
{
if(*str1 != *str2)
{
return false;
}
++str1;
++str2;
}
if(*str1 == '\0' && *str2 == '\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);
}
bool operator >= (const String s)const
{
return !(*this < s);
}
bool operator != (const String s)const
{
return !(*this == s);
}
char* c_str()
{
return _str;
}
private:
char* _str;
size_t _size;
size_t _capacity;
public:
static size_t npos;
};
size_t String::npos = -1;
}