MyString实现

// MyString.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <iostream>
#include <string>


using namespace std;


class MyString
{
public:
	//三个重载的构造函数
	MyString();
	MyString(const char* str);
	MyString(const MyString& str);
	//析构函数
	~MyString();
	
	//重载运算符
	MyString& operator = (const MyString& str);
	friend ostream& operator << (ostream& os, const MyString& str); //重载输出操作符
	friend istream& operator >> (istream& is, MyString& str);	//重载输入操作符
	friend MyString operator + (const MyString& str1,const MyString& str2); //重载加号操作符,注意返回引用不行,为啥呢?
	friend MyString operator += (MyString& str1, const MyString& str2);	//重载+=操作符
	friend bool operator == (const MyString& str1, const MyString& str2);	//重载相等操作符
	friend bool operator != (const MyString& str1, const MyString& str2);	//重载不相等操作符


	//功能函数
	int getLenth(); //取得string长度函数
	MyString substr(int pos, int len);	//求string的字串
	bool empty();	//判断string是否为空


private:
	char* p;
	int len;
};


//默认构造函数,初始化为空串
MyString::MyString()
{
	len = 0;
	p = new char[len + 1];
	p[0] = '\0';
}


//构造函数,用一个字符串初始化
MyString::MyString(const char* str)
{
	len = strlen(str);
	p = new char[len + 1];
	strcpy(p, str);
}


//复制构造函数,用另外一个string初始化
MyString::MyString(const MyString& str)
{
	len = str.len;
	p = new char[len + 1];
	strcpy(p, str.p);
}


//析构函数
MyString::~MyString()
{
	delete [] p;
}


//重载赋值操作符( = )
MyString& MyString::operator = (const MyString& str)
{
	if (this->p == str.p)
	{
		return *this;
	}
	delete [] p;
	len = str.len;
	p = new char[len + 1];
	strcpy(p, str.p);
	return *this;
}


//重载输出操作符( << )
ostream& operator << (ostream& os, const MyString& str)
{
	os << str.p;
	return os;
}


//重载输入操作符( >> )
istream& operator >> (istream& is, MyString& str)
{
	char ch;
	char c[100];
	int i(0);
	while (is >> ch)
	{
		c[i] = ch;
		i ++;	
	}
	c[i] = '\0';
	delete [] str.p;
	str.p = new char[i + 1];
	str.len = i;
	strcpy(str.p, c);
	return is;
}


//重载加号操作符( + )
MyString operator + (const MyString& str1, const MyString& str2)
{
	MyString str;
	delete [] str.p;
	str.len = str1.len + str2.len;
	str.p = new char[str.len + 1];
	strcpy(str.p, str1.p);
	strcat(str.p, str2.p);
	return str;
}


//重载相加赋值操作符( += )
MyString operator += (MyString& str1, const MyString& str2)
{
	str1 = str1 + str2;
	return str1;
}


//重载相等操作符
bool operator == (const MyString& str1, const MyString& str2)
{
	if (strcmp(str1.p, str2.p) == 0)
	{
		return true;
	}
	return false; 
}


//重载不相等操作符
bool operator != (const MyString& str1, const MyString& str2)
{
	if (strcmp(str1.p, str2.p) == 0)
	{
		return false;
	}
	return true; 
}


//取得string长度函数
int MyString::getLenth()
{
	int i(0);
	char* p = this->p;
	while (*p != NULL)
	{
		p ++;
		i ++;
	}
	return i;
}


//求string的字串
MyString MyString::substr(int pos, int count)
{
	MyString str;


	if(pos < 0||count < 0 || pos > len)             //参数不合理,不取子串
	{
		str.len = 0;
		str.p[0] = '\0';
	}
	else
	{
		if(pos + count - 1 >= this->len)                         //字符串不够长
			count = len-pos;
		str.p = new char[count + 1];
		str.len = count;
		for(int i = 0,j = pos - 1; i<count; i++, j++)
			str.p[i] = p[j];
		str.p[str.len] = '\0';
	}
	return str;
}


//判断string是否为空
bool MyString::empty()
{
	if (len == 0)
	{
		return true;
	}
	return false;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值