C++ Mystring部分实现(2)

增加实现运算符重载
增加实现

#include <iostream>
#include <cstring>


class Mystring
{
	public:
		//声明友元函数
		friend std::ostream& operator<<(std::ostream &L, Mystring &R);
		friend std::istream& operator>>(std::istream &L, Mystring &R);
		
		
		//无参构造函数
		Mystring()
		{
			str = nullptr;
			len = 0;
		}
		//有参构造函数
		Mystring(const int length, const char ch)
		{
			str = new char[length + 1];
			for(int i = 0; i < length; i++)
				str[i] = ch;
			str[length] = 0;
			len = length;
		}
		
		Mystring(const char *str)
		{
			len = strlen(str);
			this->str = new char[len + 1];
			strncpy(this->str, str, len);
		}

		Mystring(const char *str, const unsigned int &length)
		{
			len = length;
			this->str = new char[len + 1];
			strncpy(this->str, str, len);
			this->str[len] = 0;
		}

		Mystring(const Mystring &str, const unsigned int &index, const unsigned int &length)
		{
			len = length;
			this->str = new char[len + 1];
			bzero(this->str, len+1);
			strncpy(this->str, str.str+index, len);
		}

		//拷贝构造函数
		Mystring(const Mystring &str)
		{
			this->len = str.len;
			this->str = new char[this->len + 1];
			strcpy(this->str, str.str);
		}

		//拷贝赋值构造函数
		Mystring & operator=(const Mystring &str)
		{
			if(nullptr != this->str)
				delete [](this->str);
			this->len = str.len;
			this->str = new char[this->len+1];
			strcpy(this->str, str.str);
		}

		//析构函数
		~Mystring()
		{
			if(nullptr != str)
				delete []str;
		}
		
		
		//运算符重载
		//字符串相加
		const Mystring operator+(const Mystring &other)const 
		{
			Mystring st;
			st.len = this->len + other.len;
			
			if(nullptr == this->str)
			{
				if(nullptr == other.str)
					st.str = nullptr;
				else
				{
					st.str = new char[st.len + 1];
					stpcpy(st.str, other.str);
				}
			}
			else
			{
				if(nullptr == other.str)
				{
					st.str = new char[st.len + 1];
					stpcpy(st.str, this->str);
				}
				else
				{
					st.str = new char[st.len + 1];
					stpcpy(st.str, this->str);
					strcat(st.str, other.str);
				}
			}
			return st;
		}

		//字符串拼接
		Mystring &operator+=(const Mystring &other)
		{
			this->len += other.len;
			
			if(nullptr == this->str)
			{
				if(nullptr == other.str)
					this->str = nullptr;
				else
				{
					this->str = new char[this->len + 1];
					stpcpy(this->str, other.str);
				}
			}
			else
			{
				if(nullptr == other.str);
				else
				{
					char *ch = new char[this->len + 1];
					stpcpy(ch, this->str);
					strcat(ch, other.str);
					delete []this->str;
					this->str = ch;
				}
			}
			return *this;
		}
		
		//重载<
		bool operator<(const Mystring &other)const 
		{
			return strcmp(this->str, other.str)<0;
		}

		//重载<=
		bool operator<=(const Mystring &other)const 
		{
			return strcmp(this->str, other.str)<=0;
		}

		//重载>
		bool operator>(const Mystring &other)const 
		{
			return strcmp(this->str, other.str)>0;
		}

		//重载>=
		bool operator>=(const Mystring &other)const 
		{
			return strcmp(this->str, other.str)>=0;
		}

		//重载==
		bool operator==(const Mystring &other)const 
		{
			return strcmp(this->str, other.str)==0;
		}

		//重载!=
		bool operator!=(const Mystring &other)const
		{
			return strcmp(this->str, other.str)!=0;
		}
		
		
		//按下标获取字符串字符
		const char& at(int index)
		{
			if(index < 0 || index > len)
			{
				std::cout << "out_of_range" << std::endl;
				return str[0];
			}
			return str[index];
		}

		//获取字符串首地址
		const char *data()
		{
			return str;
		}

		//判断字符串是否为空
		bool empty()
		{
			if(nullptr == str)
				return true;
			return false;
		}

		//获取字符串长度
		int length()
		{
			return len;
		}

		

		


	private:
		char *str;
		unsigned int len;
};



//重载<<
std::ostream& operator<<(std::ostream &L, Mystring &R)
{
	L << R.str;
	return L;
}
//重载>>
std::istream& operator>>(std::istream &L, Mystring &R)
{
	char temp[10000]=""; 
	L.getline(temp, 10000);
	
	if(temp[R.len] == '\n')
		temp[R.len] = 0;
	
	R.len = strlen(temp);
	
	if(nullptr != R.str)
		delete []R.str;
	
	R.str = new char[R.len + 1];
	stpcpy(R.str, temp);
	
	return L;
}







int main()
{
	Mystring str;
	Mystring str1("hello");
	Mystring str2(str1);
	Mystring str3 = str2;

	
	std::cin >> str ;
	std::cout << str << std::endl;
	
	std::cout << (str < str1) << std::endl;

	


	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值