自定义string

#include "MyString.h"
//自定义string

MyString::MyString()
{
	this->len = 0;
	this->str = NULL;
}


MyString::MyString(int len)
{
	if (len == 0) {
		this->len = 0;
		this->str = new char[len + 1];
		strcpy(this->str, "");
	}
	else {
		this->len = len;

		this->str = new char[len + 1];//多一个\0空间
		memset(this->str, 0, len + 1);
	}
}

MyString::MyString(const char * str)
{
	cout << "MyString(const char * str)" << endl;
	if (str == NULL) {
		this->len = 0;
		this->str = new char[len + 1];
		strcpy(this->str, "");
	}
	else {
		int len = strlen(str);

		this->len = len;
		this->str = new char[len + 1]; //开辟str的空间长度
		strcpy(this->str, str); //将str拷贝给this->str
	}
}

MyString::MyString(const MyString& another)
{
	cout << "MyString(const MyString& another)" << endl;
	//深拷贝
	this->len = another.len;

	//拷贝str
	this->str = new char[len + 1];
	strcpy(this->str, another.str);
}

MyString &  MyString::operator=(const MyString& another)
{

	cout << " MyString::operator= ..." << endl;

	//判断是否是自身
	if (this == &another) {
		return *this;
	}

	//之前是否有垃圾回收
	if (this->str != NULL) {
		delete[]this->str;
		this->str = NULL;
		this->len = 0;
	}

	//深拷贝
	this->len = another.len;
	this->str = new char[len + 1];
	strcpy(this->str, another.str);

	//返回自身
	return *this;
}

MyString::~MyString()
{
	if (this->str != NULL) {
		cout << "~MyString()..." << endl;
		delete[]this->str;
		this->len = 0;
		this->str = NULL;
	}
}

char& MyString::operator[](int index)
{
	return this->str[index];
}

MyString & MyString::operator+=(MyString &another)
{
	//如果另一个字符串是空字符串
	if (another.len == 0) {
		return *this;
	}
	else if (this->len == 0) {
		*this = another;

		return *this;
	}

	//两边都不是空字符串
	//"123"   "abc"
	//"123abc"
	char *old_str = this->str;//将之前的str的堆内存地址保存下来,以备释放

	//求出新字符串的长度
	int len = this->len + another.len;
	
	this->str = new char[len + 1];
	strcpy(this->str, old_str);
	strcat(this->str, another.str);

	if (old_str != NULL) {
		delete[]old_str;
	}

	return *this;
}

MyString & MyString::operator += (char *str)
{
	return *this;
}

bool MyString::operator>(MyString &another)
{
	int ret = strcmp(this->str, another.str);

	if (ret > 0) {
		return true;
	}
	else {
		return false;
	}
}

bool MyString::operator<(MyString &another)
{
	int ret = strcmp(this->str, another.str);

	if (ret < 0) {
		return true;
	}
	else {
		return false;
	}
}


ostream& operator<<(ostream &os, MyString &string)
{
	if (string.str != NULL) {
		os << string.str;
	}

	return os;
}

istream &operator>>(istream &is, MyString &string)
{
	//回收之前的数据
	if (string.str != NULL) {
		delete[]  string.str;
		string.str = NULL;
		string.len = 0;
	}

	char buf[4096] = { 0 };
	cout << "请输入一个字符串小于4096" << endl;
	cin >> buf;

	string.len = strlen(buf);
	string.str = new char[string.len + 1];
	strcpy(string.str, buf);


	return is;
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值