1.String类中涉及到了拷贝构造
(1)浅拷贝
浅拷贝就是地址拷贝,拷贝之后与拷贝目标共用同一块空间
String(const String& s)
{
if (s._pStr == NULL)
{
return;
}
_pStr = s._pStr;
}
浅拷贝存在的问题:
两个指针指向同一块空间,当释放空间时将会对同一块空间释放两次导致崩溃。
(2)深拷贝
深拷贝就是重新开辟一块与拷贝目标大小相同的空间,再把目标中的元素全都拷贝过来。
String(const String& s)
{
if (s._pStr == NULL)
{
return;
}
_pStr = new char[my_strlen(s._pStr) + 1];
my_strcpy(_pStr,s._pStr);
}
2.String类代码
#include<iostream>
#include<string.h>
#include<Windows.h>
using namespace std;
class String
{
public:
String(char *pStr = "") //构造函数
{
if (pStr == NULL)
{
_pStr = new char[1];
*_pStr = '\0';
}
else
{
_pStr = new char[my_strlen(pStr) + 1];
my_strcpy(_pStr, pStr);
}
}
String(const String& s) //拷贝构造函数
{
if (s._pStr == NULL)
{
return;
}
_pStr = new char[my_strlen(s._pStr) + 1];
my_strcpy(_pStr,s._pStr);
}
~String() //析构函数
{
if (_pStr != NULL)
{
delete[]_pStr;
_pStr = NULL;
}
}
///////////////////////字符串函数自定义
int my_strlen(const char *p)
{
int count=0;
while (*p != '\0')
{
count++;
p++;
}
return count;
}
char* my_strcat(char *des, const char *src)
{
if (des == NULL)
{
cout << "参数错误" << endl;
return 0;
}
char *ret = des;
while (*des != '\0')
des++;
while (*des++ = *src++)
;
return ret;
}
char* my_strcpy(char *des, const char *src)
{
if (des == NULL)
{
MessageBeep(0);
}
char *ret = des;
while (*ret++ = *src++)
;
return des;
}
int my_strcmp(const char*dest, const char*str)
{
while (*dest == *str)
{
dest++;
str++;
}
if (*dest > *str)
return 1;
else if (*dest < *str)
return -1;
else
return 0;
}
///////////////////////////////////////
void display() //打印函数
{
cout << _pStr << endl;
}
// 不能使用库函数
String &operator=(const String& d); //赋值符重载
size_t Length(); //字符串长度
size_t Size(); //字符串所占空间大小
char& operator[](const size_t index); //重载[]
const char& operator[](const size_t index)const; //与上一行函数成对出现
bool operator>(const String& s); //重载>
bool operator<(const String& s); //重载<
bool operator==(const String& s); //重载==
bool operator!=(const String& s); //重载!=
bool strstr(const String& s); //判断在一个对象是否包含另一对象
String& operator+=(const String& s); //重载+=
String operator+(const String&s); //重载+
private:
char *_pStr;
};
String& String::operator=(const String&d)
{
if (this != &d)
{
delete [] _pStr;
_pStr = new char[my_strlen(_pStr) + 1];
my_strcpy(_pStr,d._pStr);
}
return *this;
}
size_t String::Length() //返回字符串长度
{
return my_strlen(_pStr);
}
size_t String::Size() //返回字符串所占空间大小
{
return (Length() + 1);
}
char& String::operator[](const size_t index)
{
return _pStr[index-1];
}
const char& String::operator[](const size_t index)const
{
return _pStr[index - 1];
}
//////////////////
bool String::operator>(const String& s)
{
return my_strcmp(s._pStr, _pStr) > 0 ? true : false;
}
bool String::operator<(const String& s)
{
return my_strcmp(s._pStr, _pStr) < 0 ? true : false;
}
bool String::operator==(const String& s)
{
return my_strcmp(s._pStr, _pStr) == 0 ? true : false;
}
bool String::operator!=(const String& s)
{
return my_strcmp(s._pStr, _pStr) != 0 ? true : false;
}
bool String::strstr(const String& s)
{
char*temp = s._pStr;
while (*_pStr)
{
while ((*_pStr != *s._pStr)&&(*_pStr!='\0'))
_pStr++;
char*ret = _pStr;
while ((*ret == *temp) && (*temp != '\0'))
{
ret++;
temp++;
}
if (*temp == '\0')
return true;
else
_pStr++;
}
return false;
}
String String::operator+(const String&s)
{
String d;
int len = my_strlen(_pStr) + my_strlen(s._pStr);
d._pStr = new char[len + 1];
my_strcpy(d._pStr,_pStr);
my_strcat(d._pStr, s._pStr);
return d;
}
String& String::operator+=(const String& s)
{
if (s._pStr == NULL)
return *this;
if (this == &s)
{
String copy(*this);
return *this += copy;
}
int len = my_strlen(_pStr) + my_strlen(s._pStr);
char *p_old = _pStr;
_pStr = new char[len + 1];
my_strcpy(_pStr,p_old);
my_strcat(_pStr,s._pStr);
delete []p_old;
return *this;
}
int main()
{
String t1("world!");
String t2("wormm!");
cout << "t1 : ";
t1.display();
cout << "t2 : ";
t2.display();
bool judge = (t1 > t2);
bool judge2 = (t1 < t2);
bool judge3 = (t1 == t2);
bool judge4 = (t1 != t2);
cout << judge << endl;
cout << judge2 << endl;
cout << judge3 << endl;
cout << judge4 << endl;
String t3 = t1 + t2;
t1 += t2;
cout << "t3 : ";
t3.display();
cout << "t1 : ";
t1.display();
String t4("worlsadfdjkasd!");
String t5("fhdj");
bool temp =t4.strstr(t5);
cout << temp << endl;
system("pause");
return 0;
}