模拟实现string类

namespace zwj
{
	class string
	{
	public:
		// 迭代器
		typedef char* iterator;
		typedef const char* const_iterator;

		// 迭代器
		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		const_iterator begin() const
		{
			return _str;
		}

		const_iterator end() const
		{
			return _str + _size;
		}


		// 默认构造
		string(const char* str = "")
		{
			int n = strlen(str);
			_str = new char[n + 1];
			strcpy(_str, str);
			_size = _capacity = n;
		}

		// 拷贝构造
		string(const string& str)
			: _str(nullptr)
			, _size(0)
			, _capacity(0)
		{
			string temp(str.c_str());
			swap(temp);
		}

		// 析构函数
		~string()
		{
			delete[] _str;
			_size = _capacity = 0;
		}


		// 赋值,传统写法
		//string& operator=(const string& s)
		//{
		//	if (this != &s)
		//	{

		//		//string tmp(s);
		//		//swap(tmp);
		//		char* temp = new char[s.capacity() + 1];  // 一定要记得多开一个空间,不然会报错
		//		strcpy(temp, s._str);
		//		::swap(temp, _str);
		//		_size = s.size();
		//		_capacity = s.capacity();
		//		delete[] temp;
		//	}

		//	return *this;
		//}

		 赋值,现代写法
		//string& operator=(const string& s)
		//{
		//	if (this != &s)
		//	{
		//		string tmp(s);
		//		swap(tmp);
		//	}

		//	return *this;
		//}

		string& operator=(string s)
		{
			swap(s);

			return *this;
		}


		// 扩容
		void reserve(size_t n = 0)
		{
			if (n < _size)
			{
				_size = n;
				_str[_size] = '\0';
			}
			else
			{
				char* temp = new char[n + 1];
				strcpy(temp, _str);
				::swap(temp, _str);
				delete[] temp;
				_capacity = n;
			}
		}


		// 重载+=
		string& operator+= (const char* s)
		{
			int len = strlen(s);
			if (_size + len >  _capacity)
			{
				reserve(_size + len);
			}

			strcpy(_str + _size, s);
			_size += len;

			return *this;
		}

		string& operator+= (const char ch)
		{
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : 2 * _capacity);
			}

			_str[_size++] = ch;
			_str[_size] = '\0';

			return *this;
		}

		// append追加
		string& append(const char* s)
		{
			*this += s;
			
			return *this;
		}

		string& append(const char s)
		{
			*this += s;

			return *this;
		}

		// 尾插
		void push_back(char c)
		{
			*this += c;
		}

		// 任意位置删除
		string& erase(size_t pos = 0, size_t len = npos)
		{
			assert(pos >= 0 && pos < _size);
			assert(pos != npos);

			if (len == npos || len > _size - pos)
			{
				_str[pos] = '\0';
				_size = pos;
			}
			else
			{
				size_t end = pos + len;
				while (end <= _size)
				{
					_str[end - len] = _str[end];
					end++;
				}
				_size -= len;
			}

			return *this;
		}

		// 任意位置插入字符串
		string& insert(size_t pos, const char* s)
		{
			int len = strlen(s);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}

			size_t end = _size;
			while (end >= pos)
			{
				_str[end + len] = _str[end];
				end--;

				if (end == -1) break;
			}

			strncpy(_str + pos, s, len);
			_size += len;
			return *this;
		}

		string& insert(size_t pos, const char ch)
		{
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : 2 * _capacity);
			}

			size_t end = _size;
			while (end >= pos)
			{
				_str[end + 1] = _str[end];
				end--;

				if (end == -1) break;
			}

			_str[pos] = ch;
			_size++;
			return *this;
		}

		// 交换
		void swap(string& str)
		{
			::swap(_str, str._str);
			::swap(_size, str._size);
			::swap(_capacity, str._capacity);
		}

		
		
		// []重载
		char operator[](size_t  pos) const
		{
			assert(pos >= 0 && pos < _size);

			return _str[pos];
		}

		// 返回C形式的字符串
		char* c_str() const
		{
			return _str;
		}

		// 容量
		size_t capacity() const
		{
			return _capacity;
		}

		// 大小
		size_t size() const
		{
			return _size;
		}

		// 查找字符
		size_t find(char ch, size_t pos = 0) const
		{
			assert(pos < _size);
			for (size_t i = pos; i < _size; i++)
				if (ch == _str[i]) return i;

			return npos;
		}

		// 查找字符串
		size_t find(const char* sub, size_t pos = 0) const
		{
			assert(pos < _size);
			char* ptr = strstr(_str, sub);
			if (ptr == nullptr)
				return npos;
			return ptr - _str;
		}

		// 反向查找字符
		size_t rfind(char c, size_t pos = npos) const
		{
			size_t end = pos;
			if (pos > _size)
			{
				end = _size;
			}
			
			while (end >= 0)
			{
				if (_str[end] == c) return end;
				end--;
				if (end == -1) break;
			}

			return npos;
		}

		// 反向查找字符串
		size_t rfind(const char* s, size_t pos = npos) const		
		{
			size_t end = pos;
			if (end > _size) 
				end = _size;

			int len = strlen(s);
			size_t idx = 0;
			while (idx < end)
			{
				char* ptr = strstr(_str + idx, s);
				if (idx == 0 && ptr == nullptr)
					return npos;
				
				if (ptr != nullptr)
				{
					idx = ptr - _str;
					if (idx + len == _size) 
						return idx;
				}
				else
					return idx;
			}

			return npos;
		}

		//比较运算符
		bool operator>(const string& s) const
		{
			return strcmp(_str, s._str) > 0;
		}
		bool operator==(const string& s) const
		{
			return strcmp(_str, s._str) == 0;
		}
		bool operator>=(const string& s) const
		{
			return *this > s || *this == s;
		}
		bool operator<=(const string& s) const
		{
			return !(*this > s);
		}
		bool operator<(const string& s) const
		{
			return !(*this >= s);
		}
		bool operator!=(const string& s) const
		{
			return !(*this == s);
		}

		// 生成子串
		string substr(size_t pos = 0, size_t len = npos) const
		{
			assert(pos < _size);
			if (len == npos || pos + len >= _size)
				return _str + pos;

			string s; 
			for (size_t i = pos; i <= pos + len; i++)
				s += _str[i];

			return s;
		}

	private:
		char* _str;
		size_t _size;
		size_t _capacity;

		const static size_t npos = -1;
	};


	// 重载流插入
	ostream& operator<<(ostream& out, const string& s)
	{
		for (auto e : s)
			out << e;

		return out;
	}

	// 重载流提取
	istream& operator>>(istream& in, string& s)
	{
		char ch; 
		in.get(ch);
		while (ch != ' ' && ch != '\n')
		{
			s += ch;
			in.get(ch);
		}

		return in;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_featherbrain

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

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

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

打赏作者

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

抵扣说明:

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

余额充值