代码如下:
//程序主要关注浅拷贝下的写时拷贝
#include<iostream>
#include<string.h>
#include<assert.h>
#pragma warning(disable:4996)
using namespace std;
// 1.现代写法
// 2.传统写法
// 3.字符串增删查改
class String
{
public:
String(const char* str = "")
:_str(new char [strlen(str)+1])
,_size(strlen(str))
,_capacity(strlen(str))
,_count(new int(1))
{
strcpy(_str,str);
}
// s1.Swap(s2);
void Swap(String&a,String& s)
{
swap(a._str,s._str);
swap(a._size,s._size);
swap(a._capacity,s._capacity);
}
// String s2(s1)
//String(const String& s)//拷贝构造 传统写法 深拷贝
//:_str(new char[strlen(s._str)+1])
//,_size(s._size)
//,_capacity(s._capacity)
//{
// strcpy(_str,s._str);
//}
String(const String&s)//拷贝构造 传统写法 浅拷贝
{
_str=s._str;
_size=s._size;
_capacity=s._capacity;
_count=s._count;
(*_count)++;
}
//String(const String& s)//拷贝构造 现代写法 深拷贝
//:_str(NULL)
//{
// String tmp(s._str);
// tmp._size=s._size;
// tmp._capacity=s._capacity;
// Swap((*this),tmp);
//}
// s1 = s2
String& operator=(String s)//赋值运算 浅拷贝
{
if(this!=&s)
{
_count[0]--;
_str=s._str;
s._count[0]++;
_size=s._size;
_capacity=s._capacity;
}
return *this;
}
//String& operator=(String s)//赋值运算 深拷贝
//{
// if(this!=&s)
// {
// char* newsize=new char[strlen(s._str)+1];
// delete[] _str;
// _str=newsize;
// strcpy(_str,s._str);
// _size=s._size;
// _capacity=s._capacity;
// }
// return *this;
//}
~String()
{
if(_str==""&&(*_count)==0)
{
delete[] _str;
}
}
const char* c_str()
{
return _str;
}
void Expand(size_t n)
{
if(n>_capacity)//expand
{
char* newhead=new char[n+1];
strcpy(newhead,_str);
int* zhizheng=new int(1);
// delete[] _str;
( *_count)=(*_count)-1;
_str=newhead;
_count=zhizheng;
_capacity=n;
}
}
void PushBack(char ch)//wei cha zifu
{
Insert(_size,ch);
}
void PushBack(const char* str)//wei cha zi fu chuan
{
Insert(_size,str);
}
void PopBack()//wei shan
{
if(_size==0)
{
printf("can't popback!");
}
else
{
_size--;
_str[_size]='\0';
}
}
void Insert(size_t pos, char ch)//cha ru zi fu
{
Expand(2*_capacity);
size_t i=0;
for(i=_size;i>pos;i--)
{
_str[i]=_str[i-1];
}
_str[pos]=ch;
_size+=1;
_str[_size]='\0';
}
void Insert(size_t pos, const char* str)//cha ru zi fu chuan
{
size_t len=strlen(str)+_size;
Expand(len);
size_t i=0;
for(i=len;i>pos;i--)
{
_str[i]=_str[_size--];
}
size_t j=0;
for(j=0;j<(strlen(str));j++)
{
_str[pos]=str[j];
pos++;
}
_size=len;
_str[_size]='\0';
}
void Erase(size_t pos, size_t n = 1)//shanchu
{
if(n>strlen(_str)-pos)
{
_size=pos;
}
else
{
while(n--)
{
_str[pos]=_str[pos+n];
}
_str[_size-n+1]='\0';
}
_size=_size-n;
}
size_t Find(char ch)//cha zhao zifu
{
size_t len=_size;
size_t i=0;
for(i=0;i<len;i++)
{
if(_str[i]==ch)
{
return i;
}
}
return -1;
}
size_t Find(const char* str)// cha zhao zifu chuan
{
char* head1=_str;
size_t len=strlen(str);
size_t i=0;
size_t pos=0;
while(head1)
{
for(i=0;i<len;i++)
{
if((head1[i])!=str[i])
{
head1++;
pos++;
break;
}
}
if(i==len)
{
return pos;
}
}
return -1;
}
// s1 + 'a'
String operator+(char ch)
{
String s(_str);
char* dizhi=_str;
_str=s._str;
//_count=s._count;
Insert(s._size,ch);
s._str=_str;
_str=dizhi;
return s;
}
String& operator+=(char ch)
{
String s(_str);
_str=s._str;
_size=s._size;
_capacity=s._capacity;
(*_count)--;
_count=s._count;
Insert(_size,ch);
return *this;
}
String operator+(const char* str)
{
String s(_str);
char* newhead=_str;
_str=s._str;
Insert(s._size,str);
_str=newhead;
return s;
}
String& operator+=(const char* str)
{
String s(_str);
_str=s._str;
_size=s._size;
_capacity=s._capacity;
(*_count)--;
_count=s._count;
Insert(_size,str);
return *this;
}
bool operator>(const String& s)
{
return strcmp(_str,s._str)>0;
}
bool operator>=(const String& s)
{
return (strcmp(_str,s._str)>0||strcmp(_str,s._str)==0);
}
bool operator<(const String& s)
{
return (strcmp(_str,s._str)<0);
}
bool operator<=(const String& s)
{
return (strcmp(_str,s._str)<0||strcmp(_str,s._str)==0);
}
bool operator==(const String& s)
{
return (strcmp(_str,s._str)==0);
}
bool operator!=(const String& s)
{
return (!strcmp(_str,s._str)==0);
}
private:
char* _str;
size_t _size;
size_t _capacity;
int* _count;
};
void TestString()
{
String s1("change world");
String s2(s1);
s2.PushBack('!');
cout<<s2.c_str()<<endl;
s2.PushBack("I am Coming");
cout<<s2.c_str()<<endl;
s1 += "I am Coming";
cout<<s1.c_str()<<endl;
s1.PopBack();
cout<<"s1"<<s1.c_str()<<endl;
String s3;
String s4(s3);
cout<<(s1<s2)<<endl;
s1=s2;
s1=s1;
cout<<s2.c_str()<<endl;
cout<<s1.c_str()<<endl;
s1=s1;
String a1("hello");
String a2(a1);
a1+='c';
cout<<"a1 "<<a1.c_str()<<endl;
cout<<"a2 "<<a2.c_str()<<endl;
a1+"my chance";
cout<<"a1 "<<a1.c_str()<<endl;
a1+="my chance";
cout<<"a1 "<<a1.c_str()<<endl;
}
int main()
{
TestString();
return 0;
}