模擬實現String類增刪查改

String是C++中的重要類型,程序員在C++面試中經常會遇到關於String的細節問題,甚至要求當場實現這個類。只是由於時間關係,可能只要求實現構造函數、析構函數、拷貝構造函數等關鍵部分。

String的實現涉及很多C++的基礎知識、內存控制及異常處理等問題,仔細研究起來非常複雜,本文主要做一個簡單介紹和講解模擬實現String類的增刪查改。

class String
{
public:
	String(char *str = "")
	{
		_size = strlen(str);
		_capacity = _size;
		_str = (char*)malloc(strlen(str)+1);
		strcpy(_str,str);
	}
	void Swap(String& s)
	{
		swap(_str,s._str);
		swap(_size,s._size);
		swap(_capacity,s._capacity);
	}
	//s1(s)
	String(String& s)
		:_str(NULL)
	{
		String tmp(s._str);
		Swap(s);
	}
	String& operator=(String s)
	{
		Swap(s);
		return *this;
	}
	~String()
	{
		free(_str);
	}
	char* GetStr()
	{
		return _str;
	}
	size_t Size()
	{
		return _size;
	}
	size_t Capacity()
	{
		return _capacity;
	}

//增刪查改
public:
	void PushBack(char ch)
	{
		if(_size == _capacity)
		{
			Expand(2*_size);
		}
		size_t end = _size;
		_str[end+1] = _str[end];
		_str[end] = ch;
		_size++;
	}
	void PushBack(const char* str)
	{
		assert(str);
		size_t len = strlen(str);
		if((_size+len)>_capacity)
		{
			Expand(_size+len);
		}
		size_t end = _size;
		_str[end+len] = _str[end];
		for(int i=0; i<(int)len;i++)
		{
			_str[end+i] = str[i];
		}
		_size+=len;
	}
	void PopBack(char ch)
	{
		if(_size == _capacity)
		{
			Expand(2*_size);
		}
		int end = (int)_size;
		for(int i=end; i>=0; i--)
		{
			_str[i+1] = _str[i];
		}
		_str[0] = ch;
		_size++;

	}
	void Insert(size_t pos, char ch)
	{
		if(_size == _capacity)
		{
			Expand(2*_size);
		}
		int end = (int)_size;
		for(int i=end; i>=(int)pos; i--)
		{
			_str[i+1] = _str[i];
		}
		_str[pos] = ch;
		_size++;
	}
	void Insert(size_t pos, const char* str)
	{
		assert(str);
		size_t len = strlen(str);
		if((_size+len)>_capacity)
		{
			Expand(_size+len);
		}
		int end = (int)_size;
		for(int i=end; i>=(int)pos; i--)
		{
			_str[i+len] = _str[i];
		}
		for(int i=0; i<(int)len; i++)
		{
			_str[pos+i] = str[i];
		}
		_size+=len;

	}
	void Erase(size_t pos, size_t count)
	{
		if((pos+count)>=_size)
		{
			_str[pos] = '\0';
		}
		else
		{
			int end = int(_size);
			for(int i=(pos+count);i<=(int)end; i++)
			{
				_str[i-count] = _str[i];
			}
		}
		_size-=count;
	}
	int Find(char ch) const
	{
		for(int i=0; i<=(int)_size; i++)
		{
			if(ch==_str[i])
			{
				return i;
			}
		}
		return -1;
	}
	int Find(const char* str) const
	{
		const char *des = str;
		const char *src = _str;
		const char *tmp = src;
		int i = 0;
		assert(str);
		while(*src!='\0')
		{
			if(*des == *src)
			{
				des++;
				if(*des == '\0')
				{
					return (tmp-_str);
				}
				src++;
			}
			else
			{
				tmp++;
				des = str;
				src = tmp;
			}

		}
		return -1;

	}
	char& operator[](size_t pos)
	{
		return _str[pos];
	}
	bool operator<(const String& s) const
	{
		int i = 0;
		while(1)
		{
			if((_str[i]=='\0')||(s._str[i]=='\0'))
			{
				break;
			}
			if(_str[i]>s._str[i])
			{
				return false;
			}
			if(_str[i]==s._str[i])
			{
				++i;
			}
			if(_str[i]<s._str[i])
			{
				return true;
			}
		}
		if(_str[i]<s._str[i])
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	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
	{
		int i = 0;
		while(1)
		{
			if((_str[i]=='\0')||(s._str[i]=='\0'))
			{
				break;
			}
			if(_str[i]>s._str[i])
			{
				return false;
			}
			if(_str[i]==s._str[i])
			{
				++i;
			}
			if(_str[i]<s._str[i])
			{
				return false;
			}
		}
		if((_str[i]=='\0')&&(s._str[i]=='\0'))
		{
			return true;
		}
		else
		{
			return false;
		}		
	}
	bool operator!=(const String& s)const
	{
		return !(*this==s);
	}
	void Expand(size_t n)
	{
		_str = (char*)realloc(_str,n+1);
		_capacity = n;
		if(_str==NULL)
		{
			perror("void Expand(size_t n):");
		}
	}

private:
	char* _str;
	size_t _size;//字符個數
	size_t _capacity;//容量空間
};


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章