string迭代器和运算符重载

知识点:
手动实现字符串的strlen,strcpy,strcat,strcmp重载字符串之间的+,!=,==,+=等运算符。
同样存储字符串使用的是动态数组!!!

#include<iostream>
#include <string.h>

namespace
{

	//str == str1
	//str != str1
	//str + str1
	//str += str1
	//str + "Hello"

	class String
	{
		//friend String& Strcpy(String& det, const String& src);
		//friend String& Strcat(String& det, const String& src);
		friend std::istream& operator>>(std::istream& in, String& str);
		friend std::ostream& operator<<(std::ostream& out, const String& str);
		//friend int Strlen(const String& src);
		//friend bool Strcmp(const String& det, const String& src);
	private:
		int _len;
		int _maxLen;
		char* _pArr;
	public:
		String()
		{
			_len = -1;
			_maxLen = 5;
			_pArr = new char[_maxLen];
			memset(_pArr, 0, sizeof(char) * _maxLen);
		}
		//拷贝构造
		String(const String& p)
		{
			_len = p._len;
			_maxLen = p._maxLen;
			//int len = strlen(p._pArr);
			_pArr = new char[p._maxLen];
			memset(_pArr, 0, _maxLen);
			memcpy(_pArr, p._pArr, _len + 1);
			_pArr[_len + 1] = '\0';
		}
		//有参版本的构造函数
		String(const char* value)
		{
			int len = strlen(value);
			_maxLen = 2 * (len);
			_len = len;
			_pArr = new char[_maxLen];
			memcpy(_pArr, value, len);
			_pArr[_len] = '\0';

		}
		//插入  按字符插入
		void InsertChar(const char V)
		{
			if (_len + 1 == _maxLen - 1)
			{
				_maxLen *= 2;
				char* p = new char[_maxLen];
				memset(p, 0, _maxLen);
				memcpy(p, _pArr, (sizeof(char)) * _len + 1);
				delete[] _pArr;
				_pArr = p;
			}
			_len++;
			_pArr[_len] = V;
		}
		//重载==
		int operator==(const String& src)
		{
			if (strcmp(_pArr, src._pArr) == 0)
			{
				return 0;
			}
			return 1;
		}
		//重载!=
		int operator!=(const String& src)
		{
			if (strcmp(_pArr, src._pArr) == 0)
			{
				return 1;
			}
			return 0;
		}
		//重载 +一个字符串类对象
		String operator+(const String& src)
		{
			for (int i = 0; i < strlen(src._pArr); i++)
			{
				InsertChar(src._pArr[i]);
			}
			_pArr[_len + 1] = '\0';
			return *this;
		}
		//重载 +=一个字符串类对象
		String operator+=(const String& src)
		{
			for (int i = 0; i < strlen(src._pArr); i++)
			{
				InsertChar(src._pArr[i]);
			}
			_pArr[_len + 1] = '\0';
			return *this;
		}
		//重载+一个字符串
		String operator+(const char* src)
		{
			for (int i = 0; i < strlen(src); i++)
			{
				InsertChar(src[i]);
			}
			_pArr[_len + 1] = '\0';
			return *this;
		}
		//重载+=一个字符串
		String operator+=(const char* src)
		{
			for (int i = 0; i < strlen(src); i++)
			{
				InsertChar(src[i]);
			}
			_pArr[_len + 1] = '\0';
			return *this;
		}
		//按字符输出
		void Print()
		{
			for (int i = 0; i < _len + 1; i++)
			{
				std::cout << _pArr[i] << std::endl;
			}
		}


		~String()
		{
			delete[] _pArr;
		}

	public:
	//迭代器
		class iterator
		{
			friend class String;
		private:
			char* _pArr;
			int _index;
		public:
			iterator(char* _pArr, int _index)
			{
				this->_pArr = _pArr;
				this->_index = _index;
			}
			iterator& operator++()
			{
				++_index;
				return *this;
			}
			bool  operator!=(const iterator& it)
			{
				return _index != it._index;
			}
			char& operator*()
			{
				return _pArr[_index];
			}

		};
	public:
		iterator begin()
		{
			return iterator(_pArr, 0);
		}
		iterator end()
		{
			return iterator(_pArr, _len + 1);
		}
		iterator erase(const iterator& it)
		{
			for (int i = it._index; i < _len; i++)
			{
				_pArr[i] = _pArr[i + 1];
			}
			--_len;
			return it;
		}


	};

	std::ostream& operator<<(std::ostream& out, const String& str)
	{
		out << str._pArr;
		return out;
	}
	std::istream& operator>>(std::istream& in, String& str)
	{
		in >> str._pArr;
		return in;
	}

	//String& Strcpy(String& det, const String& src)
	//{
	//	int k = 0;
	//	while (src._pArr[k] != '\0')
	//	{
	//		det._pArr[k] = src._pArr[k];
	//		k++;
	//	}
	//	//det._pArr[k] = '\0';
	//
	//	return det;
	//}
	//String& Strcat(String& det, const String& src)
	//{
	//	int k = Strlen(det);
	//	int i = 0;
	//	while (src._pArr[i] != '\0')
	//	{
	//		det._pArr[k] = src._pArr[i];
	//		i++;
	//		k++;
	//	}
	//	det._pArr[k] = '\0';
	//	return det;
	//}
	//
	//int Strlen(const String& src)
	//{
	//	int k = 1;
	//	while (src._pArr[k] != '\0')
	//	{
	//		k++;
	//	}
	//	return k;
	//}
	//
	//bool Strcmp(const String& det, const String& src)
	//{
	//	int dlen = Strlen(det);
	//	int Slen = Strlen(src);
	//	if (dlen == Slen)
	//	{
	//		for (int i = 0; i < dlen; i++)
	//		{
	//			if (det._pArr[i] != src._pArr[i])
	//			{
	//				return true;
	//			}
	//
	//		}
	//		return false;
	//	}
	//	else
	//	{
	//		return true;
	//	}
	//}
}


int main8003122()
{
	//如果下标是从-1开始的  先++后存入  而且 最大长度只能是  maxLen-1   (下标+1>=maxLen-1)
	String str;
	str.InsertChar('1');
	str.InsertChar('a');
	str.InsertChar('b');
	str.InsertChar('c');
	str.InsertChar('d');
	str.InsertChar('e');
	str.InsertChar('f');
	str.InsertChar('g');
	str.InsertChar('0');
	std::cout << str << std::endl;
	//str.erase(str.begin());
	//str.erase(str.begin());
	//str.erase(str.begin());
	//String::iterator begin = str.begin();
	//String::iterator end = str.end();
	//for (; begin != end; ++begin)
	//	std::cout << *begin << std::endl;
	String str2("1==1");
	//std::cout << str2 << std::endl;
	(str + str2);
	std::cout << str << std::endl;
	str + "999";
	std::cout << str << std::endl;
	(str += str2);
	std::cout << str << std::endl;

	//str.Print();
	return 0;
}








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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值