C++_String类的简单实现

第一次实现一个String类,没有iterator迭代器或者内存分配器等等,只是实现了简单的字符串操作并把它们写入到一个类中,重载了常见运算符提供了最简单的功能,但我认为,如果你能实现下面功能,其他功能的实现都能做到,以下代码仅供参考,可能不是最优化写法,但能跑就行,立志于写出没有BUG的代码…

#include <iostream>

using namespace std;

namespace yadong
{

	class MyString
	{
	private:
		char *_str;
		size_t _size;
	public:
		MyString(const char* str = nullptr);
		MyString(MyString&);
	public:
		MyString& operator+(const MyString&);
		MyString& operator+(const char *);
		MyString& operator=(const MyString&);
		MyString& operator=(const char *);
		char& operator[](size_t n);
		bool operator==(const MyString&);
		friend bool operator==(const char*str, const MyString&);
		friend istream& operator>>(istream& in, MyString& str);
		friend ostream& operator<<(ostream& ou, const MyString& str);
	public:
		size_t size();
		const char* c_str();
	public:
		bool compare(MyString& str);
		bool empty();
		char* substr(size_t index,size_t num = 0);
		void swap(MyString& str);

	};

	MyString::MyString(const char* str)
	{
		if (!str)
		{
			_size = 0;
			_str = nullptr;
			//_str = new char[1];
			//_str = "\0";
		}
		else
		{
			_size = strlen(str);
			_str = new char[_size + 1];
			strcpy(_str, str);
		}
	}

	MyString::MyString(MyString& str)
	{
		_size = str.size();
		_str = new char[_size + 1];
		strcpy(_str, str.c_str());
	}

	inline size_t MyString::size()
	{
		return _size;
	}

	inline const char* MyString::c_str()
	{
		return _str;
	}

	inline char& MyString::operator[](size_t n)
	{
		if (n >= _size)	return _str[_size - 1];
		return _str[n];
	}

	ostream& operator<<(ostream& ou, const MyString& str)
	{
		ou << str._str;
		return ou;
	}

	istream& operator>>(istream& in,MyString& str)
	{
		//这句释放原先内存判断是否是必须的?
		if (str._str != nullptr)	delete[] str._str;
		char *temp = new char[1024];
		in >> temp;
		str._size = strlen(temp);
		str._str = new char[str._size + 1];
		strcpy(str._str, temp);
		delete[] temp;
		temp = nullptr;
		return in;
	}

	MyString& MyString::operator+(const MyString& str)
	{
		_size = _size + str._size;
		char *newStr = new char[_size + 1];
		strcpy(newStr, _str);
		if (_str != nullptr)	delete[] _str;
		_str = strcat(newStr, str._str);
		return *this;
	}

	MyString& MyString::operator+(const char * str)
	{
		_size = _size + strlen(str);
		char *newStr = new char[_size + 1];
		strcpy(newStr, _str);
		if (_str != nullptr)	delete[] _str;
		_str = strcat(newStr, str);
		return *this;
	}

	MyString& MyString::operator=(const MyString& str)
	{
		_size = str._size;
		if (_str != nullptr)	delete[] _str;
		_str = new char[_size + 1];
		strcpy(_str, str._str);
		return *this;
	}

	MyString& MyString::operator=(const char * str)
	{
		_size = strlen(str);
		if (_str != nullptr)	delete[] _str;
		_str = new char[_size] + 1;
		strcpy(_str, str);
		return *this;
	}
	inline bool MyString::compare(MyString& str)
	{
		if (_size > str._size)	return true;
		if (_size < str._size)	return false;
		for (size_t n = 0; n < _size; n++)
		{
			if (_str[n] - str._str[n] < 0)	return false;
			if (_str[n] - str._str[n] > 0)	return true;
		}
		return true;
	}
	bool MyString::operator==(const MyString& str)
	{
		if (_size != str._size)	return false;
		for (size_t n = 0; n < _size; n++)
		{
			if (_str[n] != str._str[n])	return false;
		}
		return true;
	}
	bool operator==(const char*str, const MyString& myStr)
	{
		size_t size = strlen(str);
		if (size != myStr._size)	return false;
		for (size_t n = 0; n < size; n++)
		{
			if (str[n] != myStr._str[n])	return false;
		}
		return true;
	}
	inline bool  MyString::empty()
	{
		if (_size == 0)return true;
		else return false;
	}
	inline void MyString::swap(MyString& str)
	{
		char *tempP = _str;
		size_t tempS = _size;
		_size = str._size;
		_str = str._str;
		str._str = tempP;
		str._size = tempS;
	}
	char* MyString::substr(size_t index, size_t num)
	{
		char *str;
		if (num == 0 && num > _size - index)
			num = _size - index;
		str = new char[num + 1];
		char* tempP = &_str[index];
		tempP[num] = '\0';
		strcpy(str, tempP);
		return str;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浔汐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值