string类的模拟实现

class String
{
public:
	typedef char* Iterator;

public:
	String()
	{

	}
	String(const char* str)
	{
		if (str == nullptr)
		{
			assert(str);
			return;
		}
		_str = new char[strlen(str) + 1];
		_size = strlen(str);
		_capacity = _size+1;
		strcpy(_str,str);
	}
	String(const char* str, size_t size)//用字符串前size个字节 创建一个对象
		:_str(new char[size+1])
		, _size(size)
		, _capacity(size*3/2)
	{    
		size_t len = strlen(str);
		if (size > len)
			size = len;
		size_t i;
		for (i=0; i < size; i++)
			_str[i] = *str++;
		_str[i] = '\0';
	}
	String(const String& s)//深拷贝传统写法
		:_str(new char[strlen(s._str)+1])
	{
		strcpy(_str,s._str);
	}
	/*String(const String& s)//深拷贝现代写法
		:_str(nullptr)
	{
		String strtmp(s._str);
		swap(_str, strtmp._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;
	}
	Iterator RBegin()
	{
		return End();
	}
	Iterator REnd()
	{
		return Begin();
	}
	void PushBack(char c)
	{
		Reserve(_size + 1);
		_str[_size++] = c;
		_str[_size] = '\0';
	}
	void Append(const char* str)//追加一个字符串
	{
		size_t len = strlen(str);
		if (len > _capacity - _size)
		{
			Reserve(_capacity * 2+len);
		}
		strcat(_str,str);
		_size = _size + len;
	}
	char& operator[](size_t index)
	{
		return _str[index];
	}
	const char& operator[](size_t index)const
	{
		return _str[index];
	}
	void ReSize(size_t newSize)
	{
		if (newSize < _size)
			_size = newSize;

		else
		{
			size_t i;
			for (i = _size; i < newSize; i++)
				_str[i] = 0;
			_str[i] = '\0';
			_size = newSize;
			_capacity *= 2;
		}
	}
	void ReSize(size_t newSize, char c)//size变成newsize 多的字符用c填充
	{
		if (newSize < _size)
			_size = newSize;
		
		else
		{
			size_t i;
			for (i = _size; i < newSize; i++)
				_str[i] = c;
			_str[i] = '\0';
			_size = newSize;
			_capacity *= 2;
		}
	}
	void Reserve(size_t newCapacity)
	{
		if (newCapacity>_capacity)
			_capacity = newCapacity;
	}
	int Size()const
	{
		return _size;
	}
	bool Empty()const
	{
		return 0 == _size;
	}
	void Clear()
	{
		_size = 0;
	}
	int Find(char c, size_t pos = 0)
	{
		for (size_t i = 0; i < _size; i++)
		{
			if (c == _str[i])
				return i;
		}
		return -1;
	}
	int rFind(char c)
	{
		for (int i = _size - 1; i>0;i--)
		{
			if (c == _str[i])
				return i;
		}
		return -1;
	}
	const char* C_Str()const
	{
		return _str;
	}
	void Swap(String& s)
	{
		swap(_size, s._size);
		swap(_str, s._str);
		swap(_capacity, s._capacity);
	}
	String StrSub(size_t pos, size_t size)//从pos位置截取size个字节返回
	{
		//gfdasgfu
		size_t len = pos + size;

		if (size > len)
			size = len;

		String ret(_str + pos, size);
		return ret;
	}
	
//字符串的比较
	bool operator<(const String& s)
	{
		size_t i = 0;
		size_t len1 = strlen(this->_str);
		size_t len2 = strlen(s._str);
		if (len2<len1)
			len1 = len2;
		for (; i < len1; i++)
		{
			if (this->_str[i] < s._str[i])
				return true;
			if (this->_str[i] > s._str[i])
				return false;
		}
		if (this->_size>s._size)
			return false;
		else
			return true;
	}
	bool operator<=(const String& s)
	{
		size_t i = 0;
		size_t len1 = strlen(this->_str);
		size_t len2 = strlen(s._str);
		if (len2<len1)
			len1 = len2;
		for (; i < len1; i++)
		{
			if (this->_str[i] < s._str[i])			
				return true;			
			if (this->_str[i] > s._str[i])
				return false;
		}
		if (this->_size>s._size)
			return false;
		else
			return true;
	}
	bool operator>(const String& s)
	{
		size_t i = 0;
		size_t len1 = strlen(this->_str);
		size_t len2 = strlen(s._str);
		if (len2>len1)
			len1 = len2;
		for (; i < len1; i++)
		{
			if (this->_str[i] > s._str[i])
				return true;
			if (this->_str[i] <s._str[i])
				return false;
		}
		if (this->_size>s._size)
			return true;
		else
			return false;
	}
	bool operator>=(const String& s)
	{
		size_t i = 0;
		size_t len1 = strlen(this->_str);
		size_t len2 = strlen(s._str);
		if (len2>len1)
			len1 = len2;
		for (; i < len1; i++)
		{
			if (this->_str[i] > s._str[i])
				return true;
			if (this->_str[i] <s._str[i])			
				return false;
		}
		if (this->_size>s._size)
			return true;
		else
			return false;
	}
	bool operator==(const String& s)
	{
		size_t i = 0;
		size_t len1 = strlen(this->_str);
		size_t len2 = strlen(s._str);
		if (len2 !=len1)
			return false;
		for (; i < len1; i++)
		{
			if (this->_str[i] > s._str[i])
				return false;
			if (this->_str[i] <s._str[i])
				return false;
		}
		return true;
	}
	bool operator!=(const String& s)
	{
		size_t i = 0;
		size_t len1 = strlen(this->_str);
		size_t len2 = strlen(s._str);
		if (len2 != len1)
			return true;
		for (; i < len1; i++)
		{
			if (this->_str[i] > s._str[i])
				return true;
			if (this->_str[i] <s._str[i])
				return true;
		}
		return false;
	}
	friend ostream& operator<<(ostream& _cout, const String& s)
	{
		cout << s._str;
		return _cout;
	}
private:
	char* _str;
	size_t _capacity;
	size_t _size;
};

猜你喜欢

转载自blog.csdn.net/oldwang1999/article/details/84550751