c++-----string和深浅拷贝

时间:2022-07-20 19:49:29

深浅拷贝
浅拷贝主要存在的问题就是同一块内存释放多次。所以引入深拷贝来解决这些问题。
下面通过代码看下浅拷贝存在的问题

class String
{
public:
String(const char* str);
String(String& str);
String& operator = (String& str);
~String();
private:
char* _str;
};
String::String(const char* str = "")
{
if (str == NULL)
{
char *_str = new char[1];
_str = NULL;
}
else
{
_str = new char[strlen(str) + 1];
strcpy(_str, str);
//_str[strlen(str) + 1] = '\0';
}
}
String::String(const String& str)
:_str(str._str)
{
}
String& String::operator = (const String& str)
{
if (this != &str)
{
_str = str._str;
}
return *this;
}
String::~String()
{
if (_str != NULL)
{
delete[] _str;
_str = NULL;
}
cout << "~String()" << endl;
}
void test()
{
String s2("never give up");
String s3(NULL);
String s4(s2); // 1
s3 = s2; // 2
}
int main()
{
test();
return 0;
}

程序将无法正常运行,因为在执行析构函数的时候,一块内存释放了两次,所以程序无法显示出正确的结果。
c++-----string和深浅拷贝
为了解决浅拷贝存在的一块内存释放两次的错误操作,我们不仅要拷贝数据,也要给数据开辟空间。
传统版深拷贝

class String
{
public:
String(const char* str);
String(String& str);
String& operator = (String& str);
~String();
private:
char* _str;
};

String::String(const char* str = "")
{
if (str == NULL)
{
char *_str = new char[1];
_str = NULL;
}
else
{
_str = new char[strlen(str) + 1];
strcpy(_str, str);
//_str[strlen(str) + 1] = '\0';
}
}
String::String(const String& str)
{
char* tmp = new char[strlen(str._str) + 1]; //定义一个临时变量
strcpy(tmp, str._str);
delete[] _str; //释放原来的空间
_str = tmp; //将临时空间指向原来的空间
}
String& String::operator = (const String& str)
{
if (this == &str)
return *this;
char* tmp = new char[strlen(str._str) + 1];
strcpy(tmp,str._str);
delete[]_str;
_str = tmp;
}
String::~String()
{
if (_str != NULL)
{
delete[] _str;
_str = NULL;
}
cout << "~String()" << endl;
}
void test()
{
String s2("never give up");
String s3(NULL);
String s4(s2);
s3 = s2;
}
int main()
{
test();
return 0;
}

深拷贝实现的步骤:

  1. 释放原有的空间
  2. 从新申请合适的空间
  3. 进行拷贝
    c++-----string和深浅拷贝
    还有之中简洁版的深拷贝,它是通过交换空间和数据实现的
    代码如下
void Swap(char* s1, char* s2)
{
char* tmp = s1;
s1 = s2;
s2 = tmp;
}
String::String(String& str)
{
Swap(_str, str._str);
}
String& String::operator = (String& str)
{
if (this == &str)
{
return *this;
}
Swap(_str, str._str);
}

string类的实现

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string.h>
#include<assert.h>
using namespace std;

class String
{
public:
String();
String(const char* str);
String(const String& str);
String& operator = (const String& str);
public:
size_t Size()const;
char& operator[](size_t index);
const char& operator[](size_t index)const;
String operator+(const String& s);
bool operator>(const String& s);
bool operator<(const String& s);
bool operator==(const String& s);
bool operator!=(const String& s);
const char* C_Str()const;
String SubStr(size_t pos, size_t n);
~String();

private:
char* _str;
};

String::String()
:_str(new char[1])
{
_str = "\0";
//cout << "String()" <<this<< endl;
}
String::String(const char* str)
{
if (str == NULL)
{
_str = new char[1];
_str = "\0";
}
else
{
_str = new char[strlen(str) + 1];
strcpy(_str, str);
}
//cout << "String(const char* str)" << this << endl;
}
String::String(const String& str)
:_str(new char[strlen(str._str)+1])
{
strcpy(_str, str._str);
//cout << "String(const String& str)" << this << endl;
}
String& String::operator = (const String& str)
{
if (_str == str._str)
{
return *this;
}
if (_str != "\0")
{
delete[]_str;
}
_str = new char[strlen(str._str) + 1];
strcpy(_str, str._str);
return *this;
}
String::~String()
{
if (_str != "\0")
{
delete[] _str;
_str = NULL;
}
//cout << "~String()" << this << endl;
}

size_t String::Size()const
{
return strlen(_str);
}
char& String::operator[](size_t index)
{
return _str[index];
}
const char& String::operator[](size_t index)const
{
return _str[index];
}
String String::operator+(const String& s)
{
int i = 0;
if (s._str == NULL)
{
return *this;
}
if (this->_str == NULL)
{
return s;
}
char* _tmp = new char[s.Size()+this->Size()];
for (i = 0; i<this->Size()-1; i++)
{
_tmp[i] = _str[i];
}
for (i = this->Size()-1; i<s.Size() + this->Size(); i++)
{
_tmp[i] = s._str[i];
}
_tmp[s.Size() + this->Size()] = '\0';

return _tmp;
}
bool String::operator>(const String& s)
{
int i = 0;
int len1 = this->Size();
int len2 = this->Size();
int len;
if (s._str == NULL)
{
return true;
}
if (this->_str == NULL)
{
return false;
}
len = len1 > len2 ? len1 : len2;
for (i = 0; i < len; i++)
{
if (_str[i]>s._str[i])
{
return true;
}
else if (_str[i] < s._str[i])
{
return false;
}
else
{
continue;
}
}

}
bool String::operator<(const String& s)
{
if (*this>s||*this==s)
{
return false;
}
else
{
return true;
}

}
bool String::operator==(const String& s)
{
int i = 0;
if (this == &s)
{
return true;
}
if (this->Size() != s.Size())
{
return false;
}
else
{
for (i; i < s.Size(); i++)
{
if (_str[i] == s._str[i])
{
continue;
}
else
{
return false;
}
}
}
return true;
}
bool String::operator!=(const String& s)
{
if (*this == s)
{
return false;
}
return true;
}
const char* String::C_Str()const
{
return _str;
}
String String::SubStr(size_t pos, size_t n)
{
int i;
char* tmp = new char[n + 1];
for (i = pos; i < n; i++)
{
tmp[i] = _str[i];
}
tmp[n + 1] = '\0';
delete[] _str;
_str = tmp;
return *this;
}

int main()
{
size_t sz=0;
const char *s;
String s1;
String s2(NULL);
String s3("hello world");
String s6("helld");
String s4(s2);
s1 = s3;
//sz = s3.Size();
sz = s2.Size();
char c=s3[6];
//cout << c << endl;
//s2 = s1 + s3;
s3 > s6;
s3 < s6;
s3 == s6;
s = s6.C_Str();
cout << s << endl;
s2 = s3.SubStr(2, 5);

return 0;
}