String类

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <assert.h>
using namespace std;

class String
{
public:
	String(const char *str = " ")
		:_str(new char[strlen(str)+1])
		,_size(strlen(str))
		,_capacity(_size)
	{
		strcpy(_str, str);
	}


	//String(const String& s)
	//{//传统写法
	//	_str = new char[strlen(s._str)+1];
	//	strcpy(_str, s._str);
	//}


	String(const String& s)//现代写法
		:_size(strlen(s._str)+1)
		,_capacity(strlen(s._str)+1)
		,_str(new char[strlen(s._str)+1])
	{
		String tmp(s._str);
		swap(tmp._str, _str);
	}
	String& operator=(const String& s)
	{
		if(this != &s)
		{
			delete[] _str;
			_str = new char[strlen(s._str)+1];
			_size = s._size;
			_capacity = s._capacity;
			strcpy(_str, s._str);
		}
		return *this;
	}

	//String& operator=(String s)
	//{
	//	swap(_str, s._str);
	//	return *this;
	//}

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

	void Display()
	{
		cout<<_str<<endl;
	}

	void Expand(size_t n)
	{
		if(n > _capacity)
		{
			 _str = (char *)realloc(_str, 1+n);
		}		
		_capacity = n;
	}

	void pushback(char ch)
	{
		if(_size == _capacity)
		{
			Expand(_capacity*2);
		}
		_str[_size] = ch;
		++_size;
		_str[_size] = '\0';
	}

	void pushback(const char *str)
	{
		size_t len = strlen(str);
		if(_size == _capacity)
		{
			Expand(len+_size);
		}
		strcat(_str, str);
		_size += len;
	}

	void popback()
	{
		assert(_size);
		_str[_size-1] = '\0';
	}

	void Insert(size_t pos, char ch)//在POS位置处插一个字符ch
	{
		if(_size = _capacity)
		{
			Expand(_capacity*2);
		}
		int end = _size;
		while((size_t)end >= pos)
		{
			_str[_size+1] = _str[end];
			--end;
		}
		_str[pos] = ch;
		++_size;
	}

	void Insert(size_t pos, const char *str)//在pos位置插一个str字符串
	{
		int len = strlen(str);
		assert(str);
		if(_size+len > _capacity)
		{
			Expand(_size+len);
		}
		int end = _size;
		while((size_t)end >= pos)
		{
			_str[end+len] = _str[end];
			--end;
		}
		/*while(*str)
		{
			_str[pos++] = *str++;
		}*/
		memcpy(_str+pos,str,strlen(str));
		_size += len;
	}

	void Erase(size_t pos, size_t count)//在pos位置删除count个字符
	{
		if(pos+count > _size-1)
		{
			_str[pos] = '\0';
			_size = pos;
		}
		else
		{
			strcpy(_str+pos, _str+pos+count);
			_size -= count;
		}
	}

	size_t Find(char ch)const
	{
		size_t count = 0;
		while(count < _size)
		{
			if(_str[count] == ch)
			{
				return count;
			}
			++count;
		}
		return -1;
	}

	size_t Find(const char *str)const
	{
		assert(str);
		size_t s1 = 0;
		size_t s2 = 0;
		while(s1 < _size)
		{
			size_t s1_tmp = s1;
			while(_str[s1_tmp] == str[s2])
			{
				++s1_tmp;
				++s2;
				if(str[s2])
				{
					return s1;
				}
			}
			++s1;
			s2 = 0;
		}
		return -1;
	}

	char& operator[](size_t pos)//给[]里填一个数字就会返回对应的字母
	{
		assert(pos < _size);
		return _str[pos];	
	}

	bool operator<(const String& s)const
	{//先比较第一位的大小
		size_t i = 0;
		for(i=0; i<_size&&i<s._size; i++)
		{
			if(_str[i] < s._str[i])
			{
				return true;
			}
		}
		if(i == _size && i != s._size)
			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
	{
		return !(*this == s);
	}

	bool operator==(const String& s)const
	{
		size_t i = 0;
		while(_str[i] == s._str[i])
		{
			++i;
			if(i==strlen(_str)||i==strlen(s._str))
				break;
		}
		if(i == strlen(_str) && i == strlen(s._str))
			return true;
		else
		{
			return false;
		}
	}

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

int main()
{
	String s1("hello world");
	String s2("hello world");
	//s1.pushback(" nihaonihao");
		//s1.Display();
	//s1.popback();
	//s1.Display();
	//s1.Insert(4, "o ");
	//s1.Erase(0, 10);
	//size_t ret = s1.Find('d');
	//cout<<ret<<endl;
	//size_t ret = s1.Find("wor");
	//cout<<ret<<endl;
	//char ret = s1.operator[](3);
	//cout<<ret<<endl;
	//bool ret = s1<s2;
	//bool ret =(s1==s2);
	bool ret = (s1 != s2);
	cout<<ret<<endl;
	s1.Display();
	system("pause");
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值