C++STL:模拟实现string类

在上一篇中,我们细致说明了C++标准库中string类的常用接口及其功能。

C++STL:string类的常用接口及说明

为了更加深入的理解各个接口的使用及底层原理,我们现在就来自己模拟实现一下string类。

class String
{
public:
	typedef char* Iterator;

public:
	String(const char* str = "")
	{
		if (nullptr == str)
		{
			assert(false);
			return;
		}

		_size = strlen(str);
		_capacity = _size;
		_str = new char[_capacity + 1];
		strcpy(_str, str);
	}

	String(const String& s)
		: _str(new char[s._capacity + 1])
		, _size(s._size)
		, _capacity(s._capacity)
	{
		strcpy(_str, s._str);
	}

	String& operator=(const String& s)
	{
		if (this != &s)
		{
			char* pStr = new char[s._capacity + 1];
			strcpy(pStr, s._str);

			delete[] _str;
			_str = pStr;
			_size = s._size;
			_capacity = s._capacity;
		}

		return *this;
	}

	~String()
	{
		if (_str)
		{
			delete[] _str;
			_str = nullptr;
		}
	}

	Iterator Begin()
	{
		return _str;
	}

	Iterator End()
	{
		return _str + _size;
	}

	void PushBack(char c)
	{
		if (_size == _capacity)
			Reserve(_capacity * 2);

		_str[_size++] = c;
		_str[_size] = '\0';
	}

	void Append(size_t n, char c)
	{
		for (size_t i = 0; i < n; ++i)
			PushBack(c);
	}

	void Append(const char* str)
	{
		for (size_t i = 0; i < strlen(str); ++i)
			PushBack(str[i]);
	}

	String& operator+=(char c)
	{
		PushBack(c);
		return *this;
	}

	String& operator+=(const char* str)
	{
		for (size_t i = 0; i < strlen(str); ++i)
			PushBack(str[i]);
		return *this;
	}

	String& operator+=(const String& str)
	{
		size_t sz = str.Size();
		for (size_t i = 0; i < sz; ++i)
			PushBack(str[i]);
		return *this;
	}

	const char* C_Str() const
	{
		return _str;
	}

	void Clear()
	{
		_size = 0;
		_str[_size] = '\0';
	}

	void Swap(String& s)
	{
		swap(_str, s._str);
		swap(_size, s._size);
		swap(_capacity, s._capacity);
	}

	size_t Size() const
	{
		return _size;
	}

	size_t Capacity() const
	{
		return _capacity;
	}

	bool Empty() const
	{
		return 0 == _size;
	}

	void Resize(size_t newSize, char c = char())
	{
		if (newSize > _size)
		{
			// 若newSize大于底层空间大小,则需要重新开辟空间
			if (newSize > _capacity)
			{
				Reserve(newSize);
			}

			memset(_str + _size, c, newSize - _size);
		}

		_size = newSize;
		_str[newSize] = '\0';
	}

	void Reserve(size_t newCapacity)
	{
		// 若新容量大于旧容量,则开辟空间
		if (newCapacity > _capacity)
		{
			char* str = new char[newCapacity + 1];
			strcpy(str, _str);

			// 释放旧空间,使用新空间
			delete[] _str;
			_str = str;
			_capacity = newCapacity;
		}
	}

	char& operator[](size_t index)
	{
		assert(index < _size);
		return _str[index];
	}

	const char& operator[](size_t index) const
	{
		assert(index < _size);
		return _str[index];
	}

	// 返回c在string中第一次出现的位置
	size_t Find(char c, size_t pos = 0) const
	{
		for (size_t i = pos; i < _size; ++i)
		{
			if (c == _str[i])
			{
				return i;
			}
		}

		return string::npos;
	}

	// 返回子串s在string中第一次出现的位置
	size_t Find(const char* s, size_t pos = 0) const
	{
		size_t sz = strlen(s);

		if (sz > _size - pos)
			return string::npos;

		for (size_t i = pos; i < _size; ++i)
		{
			size_t j = 0;
			if (s[j] == _str[i])
			{
				for (j = 1; j < sz; ++j)
				{
					if (s[j] != _str[i + j])
						break;
				}
				if (j >= sz)
					return i;
			}
		}

		return string::npos;
	}

	// 截取string从pos位置开始的n个字符
	String SubStr(size_t pos, size_t n)
	{
		char* pStr = new char[n + 1];
		size_t i = 0;
		size_t j = pos;
		while (i < n)
		{
			if (j < _size)
				pStr[i++] = _str[j++];
			else
				break;
		}
		pStr[i] = '\0';

		String ret(pStr);
		return ret;
	}

	// 在pos位置上插入字符c
	String& Insert(size_t pos, char c)
	{
		String str = SubStr(pos, _size - pos);
		_str[pos] = c;
		_size = pos + 1;
		*this += str;

		return *this;
	}

	// 在pos位置上插入字符串str
	String& Insert(size_t pos, const char* str)
	{
		String pStr = SubStr(pos, _size - pos);
		_size = pos;
		*this += str;
		*this += pStr;

		return *this;
	}

	// 删除pos开始,len长度的元素
	String& Erase(size_t pos, size_t len)
	{
		String pStr = SubStr(pos + len, _size - pos - len);
		_str[pos] = '\0';
		_size = pos;
		*this += pStr;

		return *this;
	}


private:
	friend ostream& operator<<(ostream& _cout, const String& s);

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

ostream& operator<<(ostream& _cout, const String& s)
{
	cout << s._str;
	return _cout;
}

对上面模拟实现的string类进行测试,其每一个功能都能够和标准库中的string类一样,正常使用。

经过这么一个模拟实现的过程,让我也对string类有了更加深刻的理解。如果哪里有需要改进的地方,还希望各位可以指出,相互讨论学习。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值