#include<iostream>
#include<assert.h>
using namespace std;
//String类,深拷贝的增删查改
class String
{
public:
String(char* str = "")
{
_size = strlen(str);
_capacity = _size; //不含'\0'
_str = new char[_capacity + 1];
strcpy(_str, str);
}
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;
_str = NULL;
_size = _capacity = 0;
}
}
void Swap(String& s)
{
swap(_str, s._str);
swap(_size, s._size);
swap(_capacity, s._capacity);
}
char* GetStr()
{
return _str;
}
size_t Capacity()
{
return _capacity;
}
size_t Size()
{
return _size;
}
void PushBack(char ch) ; //尾插字符
void PushBack(const char* str) ; //尾插串
void PopBack() ; //尾删字符
void Insert(size_t pos, char ch) ; //定位插入字符
void Insert(size_t pos, const char* str); //定位插入串
void Erase(size_t pos, size_t count); //定位删除count个字符
size_t Find(char ch) const; //查找字符下标
size_t Find(const char* str) const; //查找串下标
char& operator[](size_t pos) ; //按下标修改字符
void Expand(size_t n) ; //指定增容为n字节,不含'\0'
bool operator<(const String& s) const; //各种运算符重载
bool operator<=(const String& s) const;
bool operator>(const String& s) const;
bool operator>=(const String& s) const;
bool operator==(const String& s) const;
bool operator!=(const String& s)const;
private:
char* _str;
size_t _size; //字符个数
size_t _capacity; //容量空间
};
void String::Expand(size_t n)
{
if(n > _capacity)
{
_str = (char*)realloc(_str, n+1);
assert(_str);
_capacity = n;
}
}
char& String::operator[](size_t pos)
{
return _str[pos];
}
void String::PushBack(char ch)
{
if(_capacity == _size)
{
Expand(_capacity * 2);
}
_str[_size++] = ch;
_str[_size] = '\0';
}
void String::PushBack(const char* str)
{
if( _size+strlen(str) > _capacity )
{
Expand(_size+strlen(str));
}
strcpy(_str+_size, str);
_size += strlen(str);
}
void String::PopBack()
{
assert(_size);
--_size;
}
void String::Insert(size_t pos, char ch)
{
if (_size == _capacity)
{
Expand(_capacity * 2);
}
//不要动_str,这是this指针会改值
size_t end = _size;
while( end >= pos )
{
_str[end+1] = _str[end];
--end;
}
_str[pos] = ch;
++_size;
}
void String::Insert(size_t pos, const char* str)
{
int 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 String::Erase(size_t pos, size_t count)
{
//1.删除的数据超出或等于原长度
if(pos+count >= _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str+pos, _str+pos+count);
_size -= count;
}
}
size_t String::Find(char ch) const
{
for(size_t i =0; i<_size; i++)
{
if(_str[i] == ch)
{
return i;
}
}
return -1;
}
bool String::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 String::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(_str[i] < s._str[i])
return true;
}
if(i==_size && i!=s._size)
return true;
else
return false;
}
bool String::operator<=(const String& s) const
{
return ((*this<s) || (*this == s));
}
bool String::operator>(const String& s) const
{
return !((*this<s) || (*this == s));
}
bool String::operator>=(const String& s) const
{
return !(*this<s);
}
bool String::operator!=(const String& s)const
{
return !(*this == s);
}
String类的增删查改(深拷贝,现代写法)
最新推荐文章于 2022-10-14 17:29:45 发布