第一次手写string

15 篇文章 0 订阅
14 篇文章 0 订阅

待优化部分:1利用string中的函数,2利用辅助函数减少代码量(代码重用没有,重复挺多的)

3,添加模板和异常,4利用转换函数减少重载

#ifndef MYSTRING_H_
#define MYSTRING_H_

#include<iostream>
#include<cstring>
#include<new>

using std::cout;
using std::cin;
using std::endl;
using std::ostream;
using std::istream;

namespace  MYSTRING
{
	class Mystring
	{
	public:
		Mystring();
		explicit Mystring(unsigned);
		explicit Mystring(const char*);
		 Mystring(const Mystring*);
		 Mystring(const Mystring&);

		~Mystring();

		const Mystring operator+(const Mystring&)const;
		const Mystring operator+(const char*)const;

		char& operator[](const unsigned);

		char& at(const unsigned);

		friend ostream& operator<<(ostream&, const Mystring&);
		friend istream& operator>>(istream&,  Mystring&);

		const bool operator==(const Mystring&)const;
		friend const bool operator==(const char*, const Mystring&);
		const bool operator!=(const Mystring&)const;
		friend const bool operator!=(const char*, const Mystring&);
		
		Mystring& operator=(const Mystring*);
		Mystring& operator=(const Mystring&);
		Mystring& operator=(const char*);
		Mystring& operator+=(const Mystring&);

		explicit operator char*()const;

		void resize(const unsigned);
	private:
		char* m_baseP;
		mutable int m_size;//有效字符数目
	};
}
#endif // !MYSTRING_H_
namespace MYSTRING
{
	//构造
	MYSTRING::Mystring::Mystring()
	{
		this->m_size = 0;
		this->m_baseP = new char[this->m_size+1]{0};
	}
	//自定义大小
	 MYSTRING::Mystring::Mystring(unsigned n)
	{
		if (n <= 0)
			exit(EXIT_FAILURE);
		this->m_size = n ;
		this->m_baseP = new char[this->m_size+1] {0};
	}
	 MYSTRING::Mystring::Mystring(const Mystring* str)
	 {
		 this->m_size = str->m_size;
		 this->m_baseP = new char[this->m_size + 1]{0};
		 for (int i = 0; i < (this->m_size); ++i)
			 (this->m_baseP)[i] = (str->m_baseP)[i];
	 }
	 MYSTRING::Mystring::Mystring(const Mystring& str)
	 {
		 this->m_size = str.m_size;
		 this->m_baseP = new char[this->m_size + 1]{0};
		 for (int i = 0; i < (this->m_size); ++i)
			 (this->m_baseP)[i] = (str.m_baseP)[i];
	 }
	//自定义大小
	void MYSTRING::Mystring::resize(const unsigned n) 
	{
		if (n <= 0)
			exit(EXIT_FAILURE);
		Mystring temp (*this);
		delete[]this->m_baseP;
		this->m_size = n;
		this->m_baseP = new char[this->m_size + 1] {0};
		unsigned size = this->m_size > n ? n : this->m_size;
		for (int i = 0; i < size; ++i)
			(this->m_baseP)[i] = (temp.m_baseP)[i];
	}
	MYSTRING::Mystring::Mystring( const char* ch)
	{
		this->m_size=strlen(ch);
		this->m_baseP = new char[this->m_size + 1]{0};
		for (int i = 0; i < (this->m_size); ++i)
			(this->m_baseP)[i] = ch[i];
	}
	//析构
	MYSTRING::Mystring::~Mystring()
	{
		delete[]this->m_baseP;
		this->m_size = 0;
	}
	//输入输出
	ostream& MYSTRING::operator<<(ostream& mycout, const Mystring& str)
	{
		mycout << str.m_baseP;
		return mycout;
	}
	istream& MYSTRING::operator>>(istream& mycin, Mystring& str)
	{
		char ch[21]{ 0 };
		delete[]str.m_baseP;
		str.m_size = 20;
		str.m_baseP = new char[str.m_size + 1] {0};
		mycin >> ch;
		for (int i = 0; i < (str.m_size ); ++i)
			(str.m_baseP)[i] = ch[i];
		return mycin;
	}
	//索引
	char& MYSTRING::Mystring::operator[](const unsigned n)
	{
		if (n < 0 || n >= this->m_size)
			exit(EXIT_FAILURE);
		return *(this->m_baseP + n);
	}
	char& MYSTRING::Mystring::at(const unsigned n)
	{
		return  (* this)[n];
	}
	//赋值
	Mystring& MYSTRING::Mystring::operator=(const Mystring* str)
	{
		delete[] this->m_baseP;
		this->m_size = str->m_size;
		this->m_baseP = new char[this->m_size + 1]{0};
		for (int i = 0; i < (this->m_size ); ++i)
			(this->m_baseP)[i] =( str->m_baseP)[i];
		return *this;
	}
	Mystring& MYSTRING::Mystring::operator=(const Mystring& str)
	{
		delete[] this->m_baseP;
		this->m_size = str.m_size;
		this->m_baseP = new char[this->m_size + 1]{0};
		for (int i = 0; i < (this->m_size); ++i)
			(this->m_baseP)[i] = (str.m_baseP)[i];
		return *this;
	}
	Mystring& MYSTRING::Mystring::operator=(const char* ch)
	{
		delete[]this->m_baseP;
		this->m_size = strlen(ch);
		this->m_baseP = new char[this->m_size + 1]{0};
		for (int i = 0; i < (this->m_size); ++i)
			(this->m_baseP)[i] = ch[i];
		return *this;
	}
	Mystring& MYSTRING::Mystring::operator+=(const Mystring& str)
	{
		*this = *this + str;
		return *this;
	}
	//运算
	const Mystring MYSTRING::Mystring::operator+(const Mystring& str) const
	{
		Mystring temp(this->m_size + str.m_size);
		int i;
		for (i = 0; i < this->m_size; ++i)
			(temp.m_baseP)[i] = (this->m_baseP)[i];
		for (int j = 0; j < str.m_size; ++j)
			(temp.m_baseP)[i++] = (str.m_baseP)[j];
		return temp;
	}
	const Mystring MYSTRING::Mystring::operator+(const char*ch) const
	{
		Mystring temp(this->m_size + strlen(ch));
		
		return temp;
	}
	//条件运算
	const bool MYSTRING::Mystring::operator==(const Mystring& str) const
	{
		if (this->m_size != str.m_size)
			return false;
		else
		{
			for (unsigned i = 0; i < this->m_size; ++i)
				if ((this->m_baseP)[i] != (str.m_baseP)[i])
					return false;
			return true;
		}
	}
	const bool MYSTRING::operator==(const char*ch, const Mystring&str)
	{
		if (strlen(ch) != str.m_size)
			return false;
		else
		{
			for (unsigned i = 0; i < (strlen(ch) > str.m_size ? strlen(ch) : str.m_size); ++i)
				if ((ch[i]) != (str.m_baseP)[i])
					return false;
			return true;
		}
	}
	const bool MYSTRING::Mystring::operator!=(const Mystring& str) const
	{
		if (*this == str)
			return false;
		else
			return true;
	}
	const bool MYSTRING::operator!=(const char* ch, const Mystring& str)
	{
		if (ch == str)
			return false;
		else
			return true;
	}
	//转换函数
	MYSTRING::Mystring::operator char* () const
	{
		char* temp = this->m_baseP;
		return temp;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值