深拷贝string类
#include <iostream>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
namespace DPcopy
{
class String
{
public:
String(const char * str = "") //不能传NULL,不然strlen就失败了
:_size(strlen(str))
, _capacity(_size)
{
_str = new char[_size + 1];//拷贝了\0
strcpy(_str, str);
}
//s2(s1)
String(const String& s)
:_str(NULL) //可以delete空值
{
String tmp(s._str);
Swap(tmp);//_str中的随机值给了tmp,析构会出错,所以置NULL
}
//s1=s3 -->s1.operator=(&s1,s3)
//String& operator=(const String& s) //缺陷
//{
// if (this != &s)
// {
// String tmp(s._str);
// //s1中已经开辟了空间
// swap(_str, tmp._str); //正好出去释放了s1中原有空间,this指针变了
// }
// return *this;
//}
//s1.Swap(s2) 为什么不用库里得swap,库里的消耗太大,是用临时对象交换的
void Swap(String& s)
{
swap(_str, s._str); //内置类型交换很低
swap(_size, s._size);
swap(_capacity, s._capacity);
}
//更简洁的写法
String& operator=(String s)
{
this->Swap(s);
return *this;
}
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] = '\0'; //size增加1
}
void Append(const char * str)
{
size_t len = strlen(str);
if (_capacity < _size + len) //需要扩容
{
//size_t newcapacity = 2 * _capacity;
//while (newcapacity < _size + len)
//{
// newcapacity*=2;
//}
Expand(_size + len);
}
strcpy(_str + _size, str);
_size += len;
}
void PopBack()
{
assert(_size > 0);
--_size;
_str[_size] = '\0';
}
void Insert(size_t pos, char ch)
{
assert(pos <= _size);
if (_capacity == _size)
{
Expand(2 * _capacity);
}
int end = _size;
while (end >= (int)pos) //会有隐式转换,end为-1也会进去
{
_str[end + 1] = _str[end];
end--;
}
_str[pos] = ch;
_size++;
}
void Insert(size_t pos, const char *str)
{
assert(pos <= _size);
if (_size == _capacity)
{
Expand(2 * _capacity);
}
int end = _size;
int len = strlen(str);
while (end >= (int)pos)
{
_str[end + len] = _str[end];
--end;
}
strncpy(_str + pos, str, len); //避免拷进\0
_size += len;
}
void Erase(size_t pos, size_t len)//删除从pos开始的len个字符
{
assert(pos < _size);
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)
{
int i = 0;
for (; i < _size; i++)
{
if (_str[i] == ch)
return i;
}
return npos;
}
size_t Find(const char* sub) const //最常用BM算法
{
char *src = _str;
while (*src)
{
char * src_tmp = src;
const char * sub_tmp = sub;
while (*sub_tmp&&*src_tmp == *sub_tmp)
{
++src_tmp;
++sub_tmp;
}
if (*sub_tmp == '\0')
{
return src - _str;
}
else
{
++src;
}
}
return npos;
}
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)
{
return !(*this >= s);
}
bool operator <=(const String& s)
{
return *this < s || *this == s;
}
bool operator >=(const String& s)
{
return !(*this < s);
}
bool operator ==(const String& s)
{
const char * str1 = _str;
const char * str2 = s._str;
while (*str1&&*str2)
{
if (*str1 != *str2)
{
return false;
}
else
{
str1++;
str2++;
}
}
if (*str1 == '\0'&&*str2 == '\0')
{
return true;
}
else
{
return false;
}
}
bool operator !=(const String& s)
{
return !(*this == s);
}
void Reserve(size_t n) //预开空间
{
Expand(n);
}
void Resize(size_t n, char ch) //size+capacity-->初始化
{
if (n <= _size)
{
_str[n] = '\0';
}
else
{
Expand(n);
int i;
for (i = _size; i < n; i++)
{
_str[i] = ch;
}
_str[n] = '\0';
}
_size = n;
}
String& operator +=(const char* str)
{
Append(str);
return *this;
}
String& operator +=(const String& s)
{
Append(s._str);
//*this += s._str;
return *this;
}
String operator +(const char* str)
{
String tmp(*this);
tmp.Append(str);
return tmp;
}
//String s=s1+"hello";
//String s;s=s1+"hello"; 代价太大,先调构造,运算符重载,赋值
String operator +(const String s)
{
return *this + s._str;
}
~String()
{
if (_str)
{
delete[] _str;
}
_str = NULL;
}
char * c_str() //返回C风格字符串有\0
{
return _str;
}
private:
size_t _size;
char* _str;
size_t _capacity;
public:
static size_t npos; //判断是否找到
};
size_t String::npos = -1;
采用引用计数进行写时拷贝(写时拷贝:需要修改的时候,把需要修改的对象拷贝出一份,进行修改,不影响其他对象的值)
class String
{
public:
String(const char *str="")
:_size(strlen(str))
, _capacity(_size)
{
_str = new char[strlen(str) + 1];
strcpy(_str, str);
_pCount = new int(1);
}
String(const String& s)
{
_str = s._str;
_pCount = s._pCount;
_size = s._size;
_capacity = s._capacity;
++(*_pCount);
}
//s1=s2
String& operator=(const String& s)
{
if (_str != s._str)
{
if (--(*_pCount) == 0)
{
delete[] _str;
delete _pCount;
}
_str = s._str;
_pCount = s._pCount;
_size = s._size;
_capacity = s._capacity;
++(*_pCount);
}
return *this;
}
void CopyOnWrite()
{
if (*_pCount > 1)
{
char *tmp = new char[strlen(_str) + 1];
strcpy(tmp, _str);
--(*_pCount);
_str = tmp;
_pCount = new int(1);
}
}
char& operator[](size_t n)
{
CopyOnWrite(); //写时拷贝
return _str[n];
}
char& operator[](size_t n) const //重载不用写时拷贝的情况
{
return _str[n];
}
String& insert(size_t pos,char ch)
{
assert(pos <= _size);
this->CopyOnWrite();
if (_size == _capacity)
Expand(2 * _capacity);
int end = _size;
while (end >= pos)
{
_str[end + 1] = _str[end];
end--;
}
_str[pos] = ch;
_str[++_size] = '\0';
return *this;
}
void Expand(size_t n)
{
if (n > _capacity)
{
char *tmp = new char[n];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
void insert(size_t pos, char * str)
{
assert(pos <= _size);
this->CopyOnWrite();
while ((_size +strlen(str))>= _capacity)
Expand(2 * _capacity);
int end = _size;
int len = strlen(str);
while (end >=(int) pos)
{
_str[end + len] = _str[end];
end--;
}
strncpy(_str + pos, str, len);
_size += len;
}
void eraser(size_t pos ,size_t n)
{
assert(pos < _size);
if (pos + n >= _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str + pos, _str + pos + n);
_size = _size - n;
}
}
~String()
{
if (--(*_pCount) == 0)
{
delete[] _str;
delete _pCount;
_pCount = NULL;
_str = NULL;
}
}
private:
size_t _size;
char *_str;
int *_pCount;
size_t _capacity;
};