string类的默认成员函数、增删查改实现
#include<iostream>
#include<assert.h>
using namespace std;
class String
{
public:
String(char* _str="")
//:p_str((char*)malloc(strlen(_str)+1))
//效果一样,但之前没考虑清楚,误打误撞对了,没注意,开辟空间应于_capacity相等
{
cout<<"构造函数"<<endl;
_size=strlen(_str);
_capacity=_size+1;
p_str=new char[_capacity];
strcpy(p_str,_str);
}
//深拷贝
String(const String& s)
:p_str(NULL)
{
cout<<"拷贝构造"<<endl;
String tmp(s.p_str);
swap(p_str,tmp.p_str);
//String(p_str);
swap(_size,tmp._size);
swap(_capacity,tmp._capacity);
}
~String()
{
cout<<"析构函数"<<endl;
if(p_str)
{
free(p_str);
p_str=NULL;
}
}
void Display()
{
cout<<p_str<<endl;
}
void CheckCapacity(size_t capacity)
{
while(capacity>_capacity)
{
_capacity+=10;
p_str=(char*)realloc(p_str,_capacity);
assert(p_str);//检查新开辟是否成功
}
}
//浅拷贝
String& operator=(const String & s)
{
if(this==&s)
{
return *this;
}
p_str=s.p_str;
//String(p_str);//为什么不可以//应该赋值给一个变量
_size=s._size;
_capacity=s._capacity;
return *this;
}
String& operator +(const String& s)//第一个&有什么用
{
//assert((p_str)&&(s.p_str));
//char* tmp=new char[_size+s._size+1];
//这儿没有检查capacity要是_size比它大了怎么办
//strcpy(tmp,p_str);
//tmp[_size]=‘\0‘;
//strcat(tmp,s.p_str);
//String str(tmp);//为什么析构了
//return str;
size_t capacity=_size+s._size+1;
CheckCapacity(capacity);
strcat(p_str,s.p_str);
String(p_str);
return *this;
}
bool operator==(String& s)
{
if(_size!=s._size)
{
return false;
}
else
{
int ret=strcmp(p_str,s.p_str);
if(ret==0)
{
return true;
}
else
{
return false;
}
}
}
bool operator!=(String& s)
{
return !(*this==s);
}
bool operator>(String& s)
{
int ret=strcmp(p_str,s.p_str);
if(ret>0)
{
return true;
}
else
{
return false;
}
}
bool operator>=(String& s)
{
return (*this>s)||(*this==s);
}
void PushBack(char ch)
{
size_t capacity=_size+2;
CheckCapacity(capacity);
p_str[_size]=ch;
p_str[_size+1]=‘\0‘;//老是忘记这步
_size+=1;
}
void PushBack(char* _str)
{
assert(_str);
size_t len=strlen(_str);
size_t capacity=_size+len+1;
CheckCapacity(capacity);
strcat(p_str,_str);
p_str[capacity]=‘\0‘;
_size+=len;
}
void PopBack(char* _str)
{
assert(_str);
}
void Insert(size_t pos,char* _str)
{
assert(pos&&_str);
size_t len=strlen(_str);
size_t capacity=_size+len+1;
CheckCapacity(capacity);
p_str[_size]=‘ ‘;
for(size_t begin=_size;begin>pos;begin--)
{
p_str[begin+len-1]=p_str[begin-1];
//(记得—1)指针下标和字符串的有效个弄混了
}
strncpy(p_str+pos,_str,len);
p_str[capacity]=‘\0‘;
_size+=len;//易忘
}
int Find(const char* _str)//注意加上const
{
size_t srcIndex=0;
size_t destIndex=0;
char* tmp=_str;
char* cur=p_str;
while((*cur!=‘\0‘)&&(destIndex!=strlen(_str)))
{
if(*cur!=*tmp)
{
cur++;
srcIndex++;
if(destIndex!=0)
{
tmp=_str;
destIndex=0;
cur--;
srcIndex--;
}
}
else
{
cur++;
srcIndex++;
tmp++;
destIndex++;
}
}
if(*cur==‘\0‘)
{
return -1;
}
else
{
return srcIndex-strlen(_str);
}
}
private:
char* p_str;
size_t _size;
size_t _capacity;
};
void Test1()
{
String s1="abc";//构造函数是什么时候析构的~建完,程序完时?
String s(s1);
s.Display();
int ret1=(s==s1);
cout<<ret1<<endl;
int ret0=(s!=s1);
cout<<ret0<<endl;
s=s+s1;
s.Display();
int ret2=(s==s1);
cout<<ret2<<endl;
/*int ret0=(s!=s1);
cout<<ret0<<endl;*/
/*int ret3=(s1>s);
cout<<ret3<<endl;*/
int ret3=(s>s1);
cout<<ret3<<endl;
int ret4=(s1>=s);
cout<<ret4<<endl;
/*int ret4=(s>=s1);
cout<<ret4<<endl;*/
}
void Test2()
{
String s="hell";
s.PushBack(‘o‘);
s.Display();
s.PushBack(" wd");
s.Display();
s.Insert(7,"orl");
s.Display();
int ret1=s.Find("el");
cout<<ret1<<endl;
int ret2=s.Find("lo");
cout<<ret2<<endl;
int ret3=s.Find("lf");
cout<<ret3<<endl;
}
int main()
{
//Test1();
Test2();
system("pause");
return 0;
}本文出自 “sunshine225” 博客,请务必保留此出处http://10707460.blog.51cto.com/10697460/1754755
原文:http://10707460.blog.51cto.com/10697460/1754755